0

I perform the following foldl operation

foldl (fn (acc,y) => if acc>y then acc else y+1) 0 [1,3]

So, I expect this to produce me an result of 4 but it produces an output of 3. What am I missing ?

My trace is something like this:

acc: 0   y: 1
acc: 2   y: 3

and since acc > y, i.e 2>3 it should go into the else branch and return 4 (3+1).

Sibi
  • 47,472
  • 16
  • 95
  • 163

2 Answers2

3

The accumulator is foldl's first parameter's second parameter. So try this:

foldl (fn (y,acc) => if acc>y then acc else y+1) 0 [1,3]

See here

isekaijin
  • 19,076
  • 18
  • 85
  • 153
2

Progression:

fn(0,1) => not(0>1) = 1+1 = 2: new acc 
fn(3,2) => is(3>2) = 3: new acc
fn([],3) => 3: final answer

foldl computes a new value immediately, foldr only begins returning a value once it has reached [].

eazar001
  • 1,572
  • 2
  • 16
  • 29