I am working through "Real World Haskell", which led to to a free PDF called "A tutorial on the universality and expressiveness of fold". It makes the point that a "fold" is "universal". I am wrestling with his definition of "universal", and would like to hear the from those who have already invested time digesting it: Please explain in ...
I was doing the exercises from YAHT's Recursive Datatype section, and found writing the listFoldr function a bit challenging (mainly because I didn't really understand the difference between foldl and foldr at first). When I finally realized exactly how the foldr function worked, I decided that a simple swap of function arguments would b...
Can anybody explain how foldr works?
Take these examples:
Prelude> foldr (-) 54 [10,11]
53
Prelude> foldr (\x y -> (x+y)/2) 54 [12,4,10,6]
12.0
I am confused about these executions, any suggestions?
...
well, this is the definition of the filter function using foldr:
myFilter p xs = foldr step [] xs
where step x ys | p x = x : ys
| otherwise = ys
so for example let's say i have this function:
myFilter odd [1,2,3,4]
so it will be:
foldr step [] [1,2,3,4]
and this will be
step 1 (foldr step [] [2,...
The code for the myAny function in this question uses foldr. It stops processing an infinite list when the predicate is satisfied.
I rewrote it using foldl:
myAny :: (a -> Bool) -> [a] -> Bool
myAny p list = foldl step False list
where
step acc item = p item || acc
(Note that the arguments to the step function are correctly ...
I wanted to test foldl vs foldr. From what I've seen you should use foldl over foldr when ever you can due to tail reccursion optimization.
This makes sense. However, after running this test I am confused:
foldr (takes 0.057s when using time command):
a::a -> [a] -> [a]
a x = ([x] ++ )
main = putStrLn(show ( sum (foldr a [] [0.. 1000...
Does anybody know the steps of haskell 'foldr' use of function?
GHCI Command Window:
foldr (\x y -> 2*x + y) 4 [5,6,7]
The result after evaluation:
40
Steps on this,
Prelude> foldr (\x y -> 2*x + y) 4 [5,6,7]
6 * 2 + (7 * 2 + 4)
12 + 18 = 30
5 * 2 + 30 = 40 v
...
Hey, im trying to declare a function, string list -> string, that with the input for instance
["Chicago","city","USA"] should return "Chigago city USA". And im having a bit of trouble,
what i did so far was this:
fun gather ts = foldr op ^ "" ts;
This seems to be somewhat along the lines, however the problem is, i would like to includ...
I've looked at http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl%27
and http://haskell.org/haskellwiki/Fold as well as a few others and they explain it fairly well.
I'm still having trouble on how a lambda would work in this case.
Example
foldr (\y ys -> ys ++ [y]) [] [1,2,3]
Could someone go through that step by step and try to e...