1) Formatting :-)
def converttobinary(n:Int, bin:List[Int]):String = {
if(n/2 == 1) (1:: (n % 2) :: bin).mkString(" ")
else {
val r = n % 2;
val q = n / 2;
converttobinary(q, r::bin)
}
}
2) Signature:
I would omit convert part, made accumulator optional parameter (function user doesn't have to supply arguments used only for internal implementation, right?)
def toBinary(n:Int, bin: List[Int] = List.empty[Int]): String = {
if(n/2 == 1) (1:: (n % 2) :: bin).mkString(" ")
else {
val r = n % 2
val q = n / 2
toBinary(q, r::bin)
}
}
Now it can be used as:
val str = toBinary(42)
Somebody might propose you to pimp such function, so call might look like
val str = 42.toBinary // btw, there is such function in std lib: 42.toBinaryString
But I don't see much profit.
The other thing that bothers me is that you're using List & mkString for this purpose, why not StringBuilder? What are the q and r?
And final minor point -- place @annotation.tailrec
to ensure that function will be optimized even in case of any future changes