0

I've got a simple recursive function to convert a list of booleans to a string:

def boolsToString(lst: List[Boolean]): String = lst match {
    case Nil => ""
    case x::xs => x match {
      case false => "0" + boolsToString(xs)
      case true => "1" + boolsToString(xs)
    }
  }

This works, but I don't like the repetition of boolsToString. I'd like to do the string concatenation just once (after the case) :

  def boolsToString2(lst: List[Boolean]): String = lst match {
    case Nil => ""
    case x::xs => x match {
      case false => "0"
      case true => "1"
    } + boolsToString2(xs)
  }

but this is rejected by the Scala compiler: "';' expected but identifier found."

Is there another way to do the string concatenation just once, after the case?

3 Answers 3

5

No need to reinvent the wheel. Iterables already have a method for joining items into a string, called mkString:

def boolsToString(lst: List[Boolean]) =
  lst.map(if(_) "1" else "0").mkString("")
Sign up to request clarification or add additional context in comments.

Comments

3

How about:

def boolsToString(lst: List[Boolean]): String = lst match {
    case Nil => ""
    case x::xs => (if (x) "1" else "0") + boolsToString(xs)
  }

Comments

1

I figured it out; the trick to get the original version working is to put extra parentheses around the body of the case:

def boolsToString2(lst: List[Boolean]): String = lst match {
    case Nil => ""
    case x::xs => (x match {
      case false => "0"
      case true => "1"
    }) + boolsToString2(xs)
}

But @Shadowlands' answer is so much nicer :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.