How does fold distinguish x from xs in Haskell? - haskell

sum' :: (Num a) => [a] -> a
sum' xs = foldl (\acc x -> acc + x) 0 xs
There is no pattern like x:xs. xs is a list. In the lambda function, how does the expression acc + x knows that x is the element in xs?

There is no pattern like x:xs. xs is a list. In the lambda function, how does the expression acc + x knows that x is the element in xs?
In Haskell - like in many programming languages - the name of a variable does not matter. For Haskell it does not matter if you write xs, or x, or acc, or use another identifier. What matters here is actually the position of the arguments.
The foldl :: (a -> b -> a) -> a -> [b] -> a is a function that takes as input a function with type a -> b -> a, followed by an object of type a, followed by a list of elements of type b, and returns an object of type a.
Semantically the second parameter of the function, will be the elements of the list. If you thus wrote \x acc -> x + acc, acc would be the eleemnts of the list, and x the accumulator.
The reason why this binds is because foldl is implemented like:
foldl f z [] = z
foldl f z (x:xs) = foldl f (f z x) xs
It thus is defined itself in Haskell, and thus binds the function to f, the initial element to z, and performs recursion to eventually obtain the result by making a recurslive call where we take the tail of the list, and use (f z x) as new initial value until the list is exhausted.
You can write the sum more elegant as:
sum' :: Num n => [n] -> n
sum' = foldl (+) 0
so here there are no explicit variables in use at all.

It doesn't "know" anything like that - there's no magic going on here.
The definition of foldl is equivalent to:
foldl f acc (x:xs) = foldl f (f acc x) xs
foldl _ acc [] = acc
So going through a simple example using your sum' function:
We start with
sum' [1,2,3]
substituting the definition of sum' we get
foldl (\acc x -> acc + x) 0 [1,2,3]
substituting the definition of foldl (first case):
foldl (\acc x -> acc + x) ((\acc x -> acc + x) 0 1) [2,3]
evaluation the function application of your lambda, we get
foldl (\acc x -> acc + x) (0 + 1) [2,3]
substituting foldl again...
foldl (\acc x -> acc + x) ((\acc x -> acc + x) (0+1) 2) [3]
and evaluating the accumulator:
foldl (\acc x -> acc + x) ((0 + 1) + 2) [3]
and substituting foldl again...
foldl (\acc x -> acc + x) ((\acc x -> acc + x) ((0 + 1) + 2) 3) []
again, evaluating the accumulator:
foldl (\acc x -> acc + x) (((0 + 1) + 2) + 3) []
now we get to the second (terminating) case of foldl because we apply it to an empty list and are left with only:
(((0 + 1) + 2 ) + 3)
which we can of course evaluate to get 6.
As you can see, there's no magic involved here: x is just a name you gave to a function argument. You could've named it user8314628 instead and it would've worked the same way. What's binding the value of the head of the list to that argument isn't any pattern matching you do yourself, but what foldl actually does with the list.
Note that you can evaluate any haskell expression using this step-by-step process; You usually won't have to, but it's useful to do this a couple of times with functions that do more-or-less complicated things and you are unfamiliar with.

how does the expression acc + x knows that x is the element in xs?
It doesn't. It computes a sum of whatever is passed to it.
Note that (\acc x -> acc + x) can be written simply as (+).

Folds take each consecutive values of the input list while making passing the remainder back to a function transparent. If you were to write your own sum’ function, you would have to pass the remainder back to your function. You would also have to pass an accumulator back to your own function to keep a running total. Fold does not make explicit the processing of a list by taking the first value and passing the remainder. What it does explicate is the accumulator. It does also have to keep a running total in the case of a sum function. The accumulator is explicit because some recursive functions may do different things with it.

Related

Haskell: Understanding the foldl function

I am learning about folds from 'Learn You a Haskell for Great Good!' by Miran Lipovaca.
For the following example which uses foldl:
sum' :: (Num a) => [a] -> a
sum' xs = foldl (\acc x -> acc + x) 0 xs
ghci> sum' [3,5,2,1]
11
I understand that acc is the accumulator and x is the starting value (the first value from the list xs). I don't quite understand how 0 and xs are passed into the lambda function as parameters - how does the function know that the value of acc is 0 and the value of x is 3? Any insights are appreciated.
Recall the definition of foldl:
foldl f acc [] = acc
foldl f acc (x:xs) = foldl f (f acc x) xs
Now, the best way to understand folds is to walk through the evaluation. So let's start with:
sum [3,5,2,1]
== foldl (\acc x -> acc + x) 0 [3,5,2,1]
The second line of the definition of the foldl function means this is equivalent to the following:
== foldl (\acc x -> acc + x) ((\acc x -> acc + x) 0 3) [5,2,1]
Now since the lambda expression is applied to parameters, 0 and 3 are passed in as acc and x:
== foldl (\acc x -> acc + x) (0+3) [5,2,1]
And the process repeats:
== foldl (\acc x -> acc + x) ((\acc x -> acc + x) (0+3) 5) [2,1]
== foldl (\acc x -> acc + x) ((0+3)+5) [2,1]
== foldl (\acc x -> acc + x) ((\acc x -> acc + x) ((0+3)+5) 2) [1]
== foldl (\acc x -> acc + x) (((0+3)+5)+2) [1]
== foldl (\acc x -> acc + x) ((\acc x -> acc + x) (((0+3)+5)+2) 1) []
== foldl (\acc x -> acc + x) ((((0+3)+5)+2)+1) []
At this point, evaluation continues according to the first line of the foldl definition:
== ((((0+3)+5)+2)+1)
So to answer your question directly: the function knows the values of acc and x simply because the definition of foldl passes their values to the function as parameters.
It would be helpful to look at how the foldl function is defined:
foldl :: (b -> a -> b) -> b -> [a] -> b
foldl f a [] = a
foldl f a (x:xs) = foldl f (f a x) xs
So, if the input list is empty then we just return the accumulator value a. However, if it's not empty then we loop. Within the loop, we update the accumulator value to f a x (i.e. we apply the lambda function f to the current accumulator value and the current element of the list). The result is the new accumulator value.
We also update the value of the list in the loop by removing its first element (because we just processed the first element). We keep processing the remaining elements of the list until there are no elements left, at which point we return the value of the accumulator.
The foldl function is equivalent to a for loop in imperative languages. For example, here's how we could implement foldl in JavaScript:
const result = foldl((acc, x) => acc + x, 0, [3,5,2,1]);
console.log(result);
function foldl(f, a, xs) {
for (const x of xs) a = f(a, x);
return a;
}
Hope that elucidates the foldl function.

How does fold works for empty list?

When we fold a list with one or more elements inside as done below:
foldr (+) 0 [1,2,3]
We get:
foldr (+) 0 (1 : 2 : 3 : [])
foldr (+) 1 + (2 +(3 + 0)) // 6
Now when the list is empty:
foldr (+) 0 []
Result: foldr (+) 0 ([])
Since (+) is binary operator, it needs two arguments to complete but here we end up (+) 0. How does it result in 0 and not throwing error of partially applied function.
Short answer: you get the initial value z.
If you give foldl or foldr an empty list, then it returns the initial value. foldr :: (a -> b -> b) -> b -> t a -> b works like:
foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
So since there are no x1, ..., xn the function is never applied, and z is returned.
We can also inspect the source code:
foldr :: (a -> b -> b) -> b -> [a] -> b
-- foldr _ z [] = z
-- foldr f z (x:xs) = f x (foldr f z xs)
{-# INLINE [0] foldr #-}
-- Inline only in the final stage, after the foldr/cons rule has had a chance
-- Also note that we inline it when it has *two* parameters, which are the
-- ones we are keen about specialising!
foldr k z = go
where
go [] = z
go (y:ys) = y `k` go ys
So if we give foldr an empty list, then go will immediately work on that empty list, and return z, the initial value.
A cleaner syntax (and a bit less efficient, as is written in the comment of the function) would thus be:
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr _ z [] = z
foldr f z (x:xs) = f x (foldr f z xs)
Note that - depending on the implementation of f - it is possible to foldr on infinite lists: if at some point f only looks at the initial value, and then returns a value, then the recursive part can be dropped.

Accumulator in foldr

In the Haskell Wikibook, foldr is implemented as follows:
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f acc [] = acc
foldr f acc (x:xs) = f x (foldr f acc xs)
It is stated that the initial value of the accumulator is set as an argument. But as I understand it, acc is the identity value for the operation (e.g. 0 for sum or 1 for product) and its value does not change during the execution of the function. Why then is it referred to here and in other texts as an accumulator, implying that it changes or accumulates a value step by step?
I can see that an accumulator is relevant in a left fold, such as foldl, but is the wikibook explanation incorrect, and only for symmetry, in which case it is wrong?
Consider the evaluation of a simple foldr expression based on the (correct) definition you provided:
foldr (+) 0 [1,2,3,4]
= 1 + foldr (+) 0 [2,3,4]
= 1 + 2 + foldr (+) 0 [3,4]
= 1 + 2 + 3 + foldr (+) 0 [4]
= 1 + 2 + 3 + 4 + foldr (+) 0 []
= 1 + 2 + 3 + 4 + 0
= 10
So you are right: acc doesn't really "accumulate" anything. It never takes on a value other than 0.
Why is it called "acc" if it isn't an accumulator? Similarity to foldl? Hysterical raisins? A lie to children? I'm not sure.
Edit: I'll also point out that the GHC implementation of foldr uses z (presumably for zero) rather than acc.
acc doesn't really accumulate anything in the case of foldr as has been pointed out.
I'd add that without it, it's not clear what should happen when the input is an empty list.
It also changes the type signature of f, limiting the functions that can be used.
E.g:
foldr' :: (a -> a -> a) -> [a] -> a
foldr' f [] = error "empty list???"
foldr' f (x:[]) = x
foldr' f (x:xs) = f x (foldr' f xs)

Haskell, Foldr, and foldl

I've been trying to wrap my head around foldr and foldl for quite some time, and I've decided the following question should settle it for me. Suppose you pass the following list [1,2,3] into the following four functions:
a = foldl (\xs y -> 10*xs -y) 0
b = foldl (\xs y -> y - 10 * xs) 0
c = foldr (\y xs -> y - 10 * xs) 0
d = foldr (\y xs -> 10 * xs -y) 0
The results will be -123, 83, 281, and -321 respectively.
Why is this the case? I know that when you pass [1,2,3,4] into a function defined as
f = foldl (xs x -> xs ++ [f x]) []
it gets expanded to ((([] ++ [1]) ++ [2]) ++ [3]) ++ [4]
In the same vein, What do the above functions a, b, c, and d get expanded to?
I think the two images on Haskell Wiki's fold page explain it quite nicely.
Since your operations are not commutative, the results of foldr and foldl will not be the same, whereas in a commutative operation they would:
Prelude> foldl1 (*) [1..3]
6
Prelude> foldr1 (*) [1..3]
6
Using scanl and scanr to get a list including the intermediate results is a good way to see what happens:
Prelude> scanl1 (*) [1..3]
[1,2,6]
Prelude> scanr1 (*) [1..3]
[6,6,3]
So in the first case we have (((1 * 1) * 2) * 3), whereas in the second case it's (1 * (2 * (1 * 3))).
foldr is a really simple function idea: get a function which combines two arguments, get a starting point, a list, and compute the result of calling the function on the list in that way.
Here's a nice little hint about how to imagine what happens during a foldr call:
foldr (+) 0 [1,2,3,4,5]
=> 1 + (2 + (3 + (4 + (5 + 0))))
We all know that [1,2,3,4,5] = 1:2:3:4:5:[]. All you need to do is replace [] with the starting point and : with whatever function we use. Of course, we can also reconstruct a list in the same way:
foldr (:) [] [1,2,3]
=> 1 : (2 : (3 : []))
We can get more of an understanding of what happens within the function if we look at the signature:
foldr :: (a -> b -> b) -> b -> [a] -> b
We see that the function first gets an element from the list, then the accumulator, and returns what the next accumulator will be. With this, we can write our own foldr function:
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f a [] = a
foldr f a (x:xs) = f x (foldr f a xs)
And there you are; you should have a better idea as to how foldr works, so you can apply that to your problems above.
The fold* functions can be seen as looping over the list passed to it, starting from either the end of the list (foldr), or the start of the list (foldl). For each of the elements it finds, it passes this element and the current value of the accumulator to what you have written as a lambda function. Whatever this function returns is used as the value of the accumulator in the next iteration.
Slightly changing your notation (acc instead of xs) to show a clearer meaning, for the first left fold
a = foldl (\acc y -> 10*acc - y) 0 [1, 2, 3]
= foldl (\acc y -> 10*acc - y) (0*1 - 1) [2, 3]
= foldl (\acc y -> 10*acc - y) -1 [2, 3]
= foldl (\acc y -> 10*acc - y) (10*(-1) - 2) [3]
= foldl (\acc y -> 10*acc - y) (-12) [3]
= foldl (\acc y -> 10*acc - y) (10*(-12) - 3) []
= foldl (\acc y -> 10*acc - y) (-123) []
= (-123)
And for your first right fold (note the accumulator takes a different position in the arguments to the lambda function)
c = foldr (\y acc -> y - 10*acc) 0 [1, 2, 3]
= foldr (\y acc -> y - 10*acc) (3 - 10*0) [1, 2]
= foldr (\y acc -> y - 10*acc) 3 [1, 2]
= foldr (\y acc -> y - 10*acc) (2 - 10*3) [1]
= foldr (\y acc -> y - 10*acc) (-28) [1]
= foldr (\y acc -> y - 10*acc) (1 - 10*(-28)) []
= foldr (\y acc -> y - 10*acc) 281 []
= 281

Haskell Fold with anonymous function

I have a problem with one of the Haskell basics: Fold + anonymous functions
I'm developing a bin2dec program with foldl.
The solution looks like this:
bin2dec :: String -> Int
bin2dec = foldl (\x y -> if y=='1' then x*2 + 1 else x*2) 0
I understand the basic idea of foldl / foldr but I can't understand what the parameters x y stands for.
See the type of foldl
foldl :: (a -> b -> a) -> a -> [b] -> a
Consider foldl f z list
so foldl basically works incrementally on the list (or anything foldable), taking 1 element from the left and applying f z element to get the new element to be used for the next step while folding over the rest of the elements. Basically a trivial definition of foldl might help understanding it.
foldl f z [] = z
foldl f z (x:xs) = foldl f (f z x) xs
The diagram from Haskell wiki might help building a better intuition.
Consider your function f = (\x y -> if y=='1' then x*2 + 1 else x*2) and try to write the trace for foldl f 0 "11". Here "11" is same as ['1','1']
foldl f 0 ['1','1']
= foldl f (f 0 '1') ['1']
Now f is a function which takes 2 arguments, first a integer and second a character and returns a integer.
So In this case x=0 and y='1', so f x y = 0*2 + 1 = 1
= foldl f 1 ['1']
= foldl f (f 1 '1') []
Now again applying f 1 '1'. Here x=1 and y='1' so f x y = 1*2 + 1 = 3.
= foldl f 3 []
Using the first definition of foldl for empty list.
= 3
Which is the decimal representation of "11".
Use the types! You can type :t in GHCi followed by any function or value to see its type. Here's what happens if we ask the for the type of foldl
Prelude> :t foldl
foldl :: (a -> b -> a) -> a -> [b] -> a
The input list is of type [b], so it's a list of bs. The output type is a, which is what we're going to produce. You also have to supply an initial value for the fold, also of type a. The function is of type
a -> b -> a
The first parameter (a) is the value of the fold computed so far. The second parameter (b) is the next element of the list. So in your example
\x y -> if y == '1' then x * 2 + 1 else x * 2
the parameter x is the binary number you've computed so far, and y is the next character in the list (either a '1' or a '0').

Resources