Home

Welcome to scala-kurz.org

  def intToNBase(base : Int)(n : Int) : List[Int] = {
    def inner(ntemp : Int, res : List[Int]) : List[Int] = {
      if(ntemp == 0) res
      else if (ntemp == 1) 1::res
      else inner(ntemp/base, ntemp%base::res)
    }
    inner(n,List())
  }

  def intToBinary(n : Int) = intToNBase(2)(n)

Not really a one-liner but very elegant in scala

Manojo (2) Visit homepage of Manojo Permalink

Tags: math (10) baseConversion (1)