ospatil (2) [Avatar] Offline
#1
The following example in the section "Passing in code so that we still honor DRY" in Chapter 3 seems incorrect -

And finally, we can express op inside fold as this...
[pre]
def foldLeft(list:Array[Int], acc:Int, op):Int = {
if (list.isEmpty) return acc
foldLeft(op(list.head), acc, list.tail, op)
}
[/pre]
While foldLeft only accepts three arguments, the recursive call on the the last line in above snippet is passing four arguments to it.
aslam.khan (50) [Avatar] Offline
#2
Re: Chapter 3 p.83 - incorrect foldLeft example
Thank you for spotting that error. The correct code is

[pre]
def foldLeft(list:Array[Int], acc:Int, op):Int = {
if (list.isEmpty) return acc
foldLeft(list.tail, op(list.head,acc), op)
}
[/pre]