Arguments not needed when using foldl in haskell?

123 Views Asked by At

What I don't get is how it is possible to use foldl in this way in haskell. I do not understand how the argument ( in this case list) is carried over implicitly:

addAll :: [Int] -> Int
addAll =  foldl (+) 0

-- This is how I could write foldl to simplify addAll where xs is clearly defined

addAll :: [Int] -> Int
addAll xs = foldl (+) 0 xs

or

addAll :: [Int] -> Int
addAll = \ xs -> foldl (+) 0 xs

But I don't really understand the first example. So basically I wonder how it is possible for something to be evaluated like that in haskell?

2

There are 2 best solutions below

4
willeM_ Van Onsem On BEST ANSWER

But I don't really understand the first example. So basically I wonder how it is possible for something to be evaluated like that in haskell?

The foldl (+) 0 produces a function. A function of type (Foldable f, Num a) => f a -> a, so why would you need an extra parameter? The addAll is a function as well.

In functional programming, functions are "first class citizens". This means that you can pass functions as parameters, and that the result can be a function. In Haskell every function takes exactly one parameter. Indeed:

foldl :: Foldable t => (b -> a -> b) ->  b ->  t a -> b

is short for:

foldl :: Foldable t => (b -> a -> b) -> (b -> (t a -> b))

foldl is thus a function that takes as parameter a function of type (b -> a -> b), and produces a function of type b -> t a -> b. This thus means that foldl (+) has type:

foldl (+) :: (Foldable f, Num b) => b -> (f b -> b)

again a function that in this case takes a parameter the base case for foldl, and returns then a function that maps a (Foldable f, Num a) => f a -> f a. If you write foldl (+) 0, this is thus short for (fold (+)) 0.

0
chepner On

Remember that all functions in Haskell are curried. foldl is no exception; it has type Foldable t => (b -> a -> b) -> b -> t a -> b, which means it takes an argument of type (b -> a -> b) and returns a function of type Foldable t => b -> t a -> b.

That function is also curried. foldl (+) takes an argument of type Int b => b and returns a function of type (Foldable t, Num b) => t b -> b.

Thus, foldl (+) 0 has type (Foldable t, Num b) => t b -> b, and your type annotation makes the restrictions t ~ [] and b ~ Int.