0

Why is the accumulator AccIn not the left parameter for Fun ? http://erlang.org/doc/man/lists.html#foldl-3

foldl(Fun, Acc0, List) -> Acc1


Fun = fun((Elem :: T, AccIn) -> AccOut)
Acc0 = Acc1 = AccIn = AccOut = term()
List = [T]
T = term()

I ask this because nearly every other functional language (e.g. haskell, scala) has it the the other way round. You're meant to visualize a left fold as accumulating from the left as

foldl f z [x1, x2, ..] = ((z f x1) f x2) ..

sa___
  • 363
  • 2
  • 12

2 Answers2

0

It's the same as in Haskell:

foldl(Fun, Acc0, List) -> Acc1

foldl :: (a -> b -> a) -> a -> [b] -> a

Community
  • 1
  • 1
  • No. I think you missed the spec for erlang's foldl. Try comparing that with the haskell-version. – sa___ Aug 13 '17 at 06:03
  • What you've copied is the haskell spec. `(a -> b -> a)` has `a` as the same type as the accumulator. Now compare that to erlang's which is more of the kind `(b -> a -> a)` and you'd get what i'm talking about. – sa___ Aug 25 '17 at 16:13
0

Consistency of argument order between the different versions of folds was simply considered more important than any such algebraic visualization as you mention.

RichardC
  • 10,412
  • 1
  • 23
  • 24