I was going through "Chapter 16 of Programming in Scala, First Edition" and looking at the use of fold left operation (/:) to efficiently reverse a list. I got a surprising behavior difference between foldLeft and it's short form (/:). Though the scala API and the book says that both functions are one and the same they both give different results. I tried it in both scala worksheet and REPL.
val xs = List(1,2,3,4,5) //> xs : List[Int] = List(1, 2, 3, 4, 5)
def reverseLeft[T](xs: List[T]) =
(List[T]() /: xs) {(ys, y) => y :: ys} //> reverseLeft: [T](xs: List[T])List[T]
def reverseLeft1[T](xs: List[T]) =
(List[T]() foldLeft xs) {(ys, y) => y :: ys} //> reverseLeft1: [T](xs: List[T])List[T]
reverseLeft(xs) //> res0: List[Int] = List(5, 4, 3, 2, 1)
reverseLeft1(xs) //> res1: List[Int] = List(1, 2, 3, 4, 5)
So, using foldLeft is not giving me the correct behaviour here. reverseLeft1() is not reversing the list but reverseLeft() is.
Please suggest if I am missing something here.