Could you explain me step by step the result of the second instruction?
I know how foldr works in this cases:
foldr (*) 1 [-3..-1]
-6
But I don't know how to deal with the function (\y z -> y*3 + z) in a foldr expression.
foldr (\y z -> y*3 + z) 0 [1..4]
30
Let's look at the definition of foldr:
foldr f z [] = z
foldr f z (x:xs) = f x (foldr f z xs)
Now, in your example,
f y z = y*3 + z
So, just using the definitions:
foldr f 0 [1..4] =
f 1 (foldr f 0 [2..4]) =
f 1 (f 2 (foldr f 0 [3,4])) =
f 1 (f 2 (f 3 (foldr f 0 [4]))) =
f 1 (f 2 (f 3 (f 4 (foldr f 0 [])))) =
f 1 (f 2 (f 3 (f 4 0))) =
f 1 (f 2 (f 3 12))) =
f 1 (f 2 21) =
f 1 27 =
30
Related
Since I am new in "innermost, outermost" I have problem with understanding the leftmost outermost style.
I would like to understand the reduction processes for the list [5,2,1]
foldl :: ( b -> a -> b ) -> b -> [ a ] -> b
foldl _ e [] = e
foldl f e (x:xs) = foldl f (f e x) xs
foldl (\acc x -> acc ++ [negate x]) [] [5,2,1]
You can inline the definition to get a better understanding of what is going on.
foldl (\acc x -> acc ++ [negate x]) [] [5,2,1]
-- using foldl f e (x:xs) = foldl f (f e x) xs
-- with f = (\acc x -> acc ++ [negate x])
-- e = []
-- x = 5
-- xs = [2,1]
-- replace line 1 with foldl f (f e x) xs
foldl f (f [] 5) [2,1]
foldl f (f (f [] 5) 2) [1]
foldl f (f (f (f [] 5) 2) 1) []
-- using foldl _ e [] = e
f (f (f [] 5) 2) 1
-- in infix style (f [] 5 == [] `f` 5)
(([] `f` 5) `f` 2) `f` 1
In general
foldl (+) 0 [a, b, c] == ((0 + a) + b) + c
foldr (+) 0 [a, b, c] == a + (b + (c + 0))
This question already has an answer here:
Haskell foldr with function
(1 answer)
Closed 5 years ago.
I just have a question about the usage of foldr.
Say I want to use foldr in this way:
foldr (\x y -> (x + y)/2) 2 [4,5,6]
Is y in this case represents each element of the list?
In particular, let's unwrap this function.
we have (x = 2, y= 6) -> (2+6)/2 = 4.
Next, do we have x=4, y = 5?
I am asking a simplify version of what I really want to ask. Here, the content of x and y represents matter a lot because in my application, I have a function that takes 1st argument as type a, and second argument as type b. Thus, I do need to know whats going on under-the hood.
Say I want to use foldr in this way:
foldr (\x y -> (x + y)/2) 2 [4,5,6]
Is y in this case represents each element of the list?
One way of figuring it out is looking at the type of foldr:
GHCi> :t foldr
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
The structure being folded has type t a for some Foldable t (in your case, you have a list, and so t is []), and its elements have type a. The result accumulated through the fold has type b. The binary function used for folding has type a -> b -> b, so the elements are passed as its first argument, and the accumulated value, as its second.
Another way of figuring out is by reading the actual implementation of foldr for lists (I found this by looking for the Foldable instance of lists and then following the source links). You might want to try that, to see how one picture corresponds to the other.
foldr f startingValue (x:xs) expands to f x (foldr f startingValue xs), and foldr f startingValue [] expands to startingValue so in your case:
foldr (\x y -> (x + y)/2) 2 [4,5,6]
(\x y -> (x + y)/2) 4 (foldr (\x y -> (x + y)/2) 2 [5,6])
(\x y -> (x + y)/2) 4 ((\x y -> (x + y)/2) 5 (foldr (\x y -> (x + y)/2) 2 [6]))
(\x y -> (x + y)/2) 4 ((\x y -> (x + y)/2) 5 ((\x y -> (x + y)/2) 6 (foldr (\x y -> (x + y)/2) 2 [])))
(\x y -> (x + y)/2) 4 ((\x y -> (x + y)/2) 5 ((\x y -> (x + y)/2) 6 2))
(\x y -> (x + y)/2) 4 ((\x y -> (x + y)/2) 5 4)
(\x y -> (x + y)/2) 4 4.5
4.25
It might be easier to see with f x y = (x + y)/2:
foldr f 2 [4,5,6]
f 4 (foldr f 2 [5,6])
f 4 (f 5 (foldr f 2 [6]))
f 4 (f 5 (f 6 (foldr f 2 [])))
f 4 (f 5 (f 6 2))
f 4 (f 5 4)
f 4 4.5
4.25
If you understand how cons lists are constructed (using :), you can think of foldr as replacing all the : with the function you provide.
I am new to haskell and I would like you to give me your suggestion about the following haskell expression evaluation.
f xs = foldr (\x n->n+1) 0 xs
function
f [1, 4]
Evaluation
(\x n->n+1) 1 (foldr (\x n->n+1) 0 [4])
= (foldr (\x n->n+1) 0 [4]) + 1
= ((\x n->n+1) 4 (foldr (\x n->n+1) 0 [4])) + 1
= (foldr (\x n->n+1) 0 [] + 1) + 1
= (0 + 1) + 1
= 1 + 1
= 2
First of all we need the foldr implementation:
foldr f z [] = z
foldr f z (x:xs) = f x (foldr f z xs)
now let's try to "trace" our computation f [1, 4]:
f [1, 4] -- rewrite list
f (1:(4:[])) -- replace f function where xs = (1:(4:[]))
foldr (\x n-> (+) n 1 ) 0 (1:(4:[]))
-- using foldr f z (x:xs) = f x (foldr f z xs)
-- where f = (\x n -> (+) n 1), z = 0, x = 1, xs = (4:[])
(\x n ->(+) n 1) 1 ( foldr (\x n -> (+) n 1) 0 (4:[]))
-- lambda function will be evaluated with x = 1, n = ( foldr (\x n -> n+1) 0 (4:[]))
(+) (foldr (\x n -> n+1) 0 (4:[])) 1
-- (+) will try to evaluate first parameter
-- foldr will be applied with f = (\x n -> (+) n 1), z = 0, x = 4, xs = []
(+) ((\x n -> (+) n 1) 4 (foldr (\x n -> (+) n 1) 0 [] )) 1
-- now again the lambda will be evaluated where x = 4 and n = (foldr (\x n -> (+) n 1) 0 [] )
(+) ( (+) (foldr (\x n -> (+) n 1) 0 [] ) 1 ) 1
-- now foldr will be evaluated again but this time first form will be used, so just z counts: f = (\x n -> (+) n 1), z = 0, [] = []
(+) ( (+) ( 0 ) 1 ) 1
-- now just go with the flow
(+) ( (+) 0 1 ) 1
(+) ( 1 ) 1
(+) 1 1
2
Given the following example
foldr(\ x y -> ........
if the input is a list for example [1,2,3]
what is x and what is y?
Let's take a look at the type for foldr.
foldr :: (a -> b -> b) -> b -> [a] -> b
Since you're providing the function that uses x and y, you can see from the type that x will be a value from your list ([1,2,3]), and y must be the accumulator value, which you initialize with the second parameter to foldr.
The definition of foldr is
foldr f z [] = z
foldr f z (x:xs) = f x (foldr f z xs)
You can just apply it directly in your example:
foldr (\x y -> foo x y) z [1,2,3]
=
(\x y -> foo x y) 1 (foldr (\x y -> foo x y) z [2,3])
=
foo 1 (foldr (\x y -> foo x y) z [2,3])
So x is 1 and y is foldr (\x y ...) z [2,3]).
In general you can think of foldr f z as replacing every (:) in a list with f, and the [] with z. So foldr f z [a,b,c,d] = f a (f b (f c (f d z))) (since [a,b,c,d] = (:) a ((:) b ((:) c ((:) d []))).
Let's say I have the following function:
sumAll :: [(Int,Int)] -> Int
sumAll xs = foldr (+) 0 (map f xs)
where f (x,y) = x+y
The result of sumAll [(1,1),(2,2),(3,3)] will be 12.
What I don't understand is where the (x,y) values are coming from. Well, I know they come from the xs variable but I don't understand how. I mean, doing the code above directly without the where keyword, it would be something like this:
sumAll xs = foldr (+) 0 (map (\(x,y) -> x+y) xs)
And I can't understand, in the top code, how does the f variable and (x,y) variables represent the (\(x,y) -> x+y) lambda expression.
Hopefully this will help. The key is that f is applied to the elements of the list, which are pairs.
sumAll [(1,1),(2,2),(3,3)]
-- definition of sumAll
= foldr (+) 0 (map f [(1,1),(2,2),(3,3)])
-- application of map
= foldr (+) 0 (f (1,1) : map f [(2,2),(3,3)])
-- application of foldr
= 0 + foldr (+) (f (1,1)) (map f [(2,2),(3,3)])
-- application of map
= 0 + foldr (+) (f (1,1)) (f (2,2) : map f [(3,3)])
-- application of foldr
= 0 + (f (1,1) + foldr (+) (f (2,2)) (map f [(3,3)]))
-- application of f
= 0 + (2 + foldr (+) (f (2,2)) (map f [(3,3)]))
-- application of map
= 0 + (2 + foldr (+) (f (2,2)) (f (3,3) : map f []))
-- application of foldr
= 0 + (2 + (f (2,2) + foldr (+) (f (3,3)) (map f [])))
-- application of f
= 0 + (2 + (4 + foldr (+) (f (3,3)) (map f [])))
-- application of map
= 0 + (2 + (4 + foldr (+) (f (3,3)) []))
-- application of foldr
= 0 + (2 + (4 + f (3,3)))
-- application of f
= 0 + (2 + (4 + 6))
= 0 + (2 + 10)
= 0 + 12
= 12
In Haskell, functions are first class datatypes.
This means you can pass functions around like other types of data such as integers and strings.
In your code above you declare 'f' to be a function, which takes in one argumenta (a tuple of two values (x,y)) and returns the result of (x + y).
foldr is another function which takes in 3 arguments, a binary function (in this case +) a starting value (0) and an array of values to iterator over.
In short 'where f (x,y) = x + y' is just scoped shorthand for
sumAll :: [(Int,Int)] -> Int
sumAll xs = foldr (+) 0 (map myFunctionF xs)
myFunctionF :: (Int,Int) -> Int
myFunctionF (x,y) = x + y
Edit: If your unsure about how foldr works, check out Haskell Reference Zvon
Below is an example implementation of foldl / map.
foldl :: (a -> b -> b) -> b -> [a] -> b
foldl _ x [] = x
foldl fx (y:ys) = foldl f (f y x) ys
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = (f x) : (map f xs)
Not an answer, but I thought I should point out that your function f:
f (x, y) = x + y
can be expressed as
f = uncurry (+)