I am working on scala labs material and I am creating a function that will eventually return something like this: tails(List(1,2,3,4)) = List(List(1,2,3,4), List(2,3,4), List(3,4), List(4), List())
I got this working using two functions and using some recursion on the second.
def tails[T](l: List[T]): List[List[T]] = { if ( l.length > 1 )trailUtil(List() ::: List(l)) else List() ::: List(l); } def trailUtil[T](l:List[List[T]]) : List[List[T]] = { if ( l.last.length == 0)l else trailUtil(l :+ l.last.init); }
All this is good, but it pushes me to the fact that for this I need two functions. I tried switching: trailUtil(List() ::: List(l)) for an anonymous function, but I got this error type mismatch; found :List[List[T]] required:Int type mismatch; found :List[List[T]] required:Int from the IDE.
val ret : List[List[T]] = (ll:List[List[T]]) => { if ( ll.last.length == 0) ll else ret(ll :+ ll.last.init) } ret(List() ::: List(1))
Can someone tell me what I'm doing wrong, or is the best way to do this, which would be great.
(I watched this one , but different types just don't work for me):
source share