Reputation: 17
I have a code
g :: Int->Int->Int
g x y = x*2 - y
then If i call foldl1 g [4,3,2,1] it returns 15, but i don't get how it returns 15, can anyone explain me why this is the case?
Upvotes: 0
Views: 2795
Reputation: 80915
foldl1
first applies the function to the first two elements of the list, then takes the result and applies the function to it and the third element, then takes the result and applies the function to it and the fourth element, then to result and fifth element, then sixth element, and so on until the list ends.
So:
Step 1: g 4 3 = 4*2 - 3 = 5
Step 2: g 5 2 = 5*2 - 2 = 8
Step 3: g 8 1 = 8*2 - 1 = 15
Upvotes: 5