Scala, P08 - pack
I'm doing the packing problem from P08 from the 99 Problems, I think I'm
doing this right but there's something wrong with the syntax of Scala that
I think I don't know..
def pack(list: List[Char]): List[List[Char]] = {
@tailrec
def checkNext(a: List[List[Char]], prev: Char, l: List[Char] ):
List[List[Char]] = {
if (!l.nonEmpty) a
else {
val res = if (prev==l.head) List(a.head:::List(l.head)) else
a:::List(List(l.head))
checkNext(res, l.head, l.tail)
}
}
checkNext(List(List(list.head)), list.head, list.tail)
}
No comments:
Post a Comment