I wrote my own recursive definition of a foldLeft
and I'd like to use it with this function joinTerminateLeft
which takes a list of strings and a terminator and creates a new string with those strings all separated by the terminator.
For example List("a", "b", "c", "d")
with terminator ;
would end up being a;b;c;d;
Here is my foldLeft
which I think is fine but my terminateLeft
doesn't work for some odd reason, any idea?
def foldLeft [A,B] (xs:List[A], e:B, f:(B,A)=>B) : B = {
def auxFoldLeft(xs: List[A], e: B) : B = {
xs match {
case Nil => e
case x::xs => auxFoldLeft(xs, f(e, x))
}
}
auxFoldLeft(xs, e)
}
def joinTerminateLeft (xs : List[String], term : String) : String = {
def f(s: String, s2: String) : String = s + s2
xs match {
case Nil => ""
case x::xs => x + foldLeft(xs, term, f)
}
}
When I run joinTerminateLeft
with a,b,c,d it stops after B for some reason and outputs the strings c,d but not with the terminator.