In the following code snippet I'm using reduceLeft and foreach loop to find the sum of the differences of a number against all of the list members. I was expecting that the result from these two would be the same (1050) but reduceLeft is adding extra 50 (val x) in final answer. What is the reason behind that?
val list = List(200,400,600)
val x = 50
println(list.reduceLeft((total, cur) => total + Math.abs(x - cur)))
var total = 0l
list.foreach(p => {
total = total + Math.abs(x - p)
})
println(total)