fold

What constitutes a fold for types other than list?

你离开我真会死。 提交于 2019-11-26 08:42:34
问题 Consider a single-linked list. It looks something like data List x = Node x (List x) | End It is natural to define a folding function such as reduce :: (x -> y -> y) -> y -> List x -> y In a sense, reduce f x0 replaces every Node with f and every End with x0 . This is what the Prelude refers to as a fold . Now consider a simple binary tree: data Tree x = Leaf x | Branch (Tree x) (Tree x) It is similarly natural to define a function such as reduce :: (y -> y -> y) -> (x -> y) -> Tree x -> y

Reduce, fold or scan (Left/Right)?

旧时模样 提交于 2019-11-26 07:51:03
问题 When should I use reduceLeft , reduceRight , foldLeft , foldRight , scanLeft or scanRight ? I want an intuition/overview of their differences - possibly with some simple examples. 回答1: In general, all 6 fold functions apply a binary operator to each element of a collection. The result of each step is passed on to the next step (as input to one of the binary operator's two arguments). This way we can cumulate a result. reduceLeft and reduceRight cumulate a single result. foldLeft and foldRight

foldl is tail recursive, so how come foldr runs faster than foldl?

感情迁移 提交于 2019-11-26 06:58:37
问题 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.. 100000]))) foldl (takes 0.089s when using time command): b::[b] -> b -> [b] b xs = ( ++ xs). (\\y->[y]) main = putStrLn(show ( sum (foldl b [] [0.. 100000]))) It\'s

Implications of foldr vs. foldl (or foldl')

狂风中的少年 提交于 2019-11-26 06:11:07
问题 Firstly, Real World Haskell , which I am reading, says to never use foldl and instead use foldl\' . So I trust it. But I\'m hazy on when to use foldr vs. foldl\' . Though I can see the structure of how they work differently laid out in front of me, I\'m too stupid to understand when \"which is better.\" I guess it seems to me like it shouldn\'t really matter which is used, as they both produce the same answer (don\'t they?). In fact, my previous experience with this construct is from Ruby\'s

Writing foldl using foldr

雨燕双飞 提交于 2019-11-26 04:59:17
问题 In Real World Haskell , Chapter 4. on Functional Programming: Write foldl with foldr: -- file: ch04/Fold.hs myFoldl :: (a -> b -> a) -> a -> [b] -> a myFoldl f z xs = foldr step id xs z where step x g a = g (f a x) The above code confused me a lot, and somebody called dps rewrote it with a meaningful name to make it a bit clearer: myFoldl stepL zeroL xs = (foldr stepR id xs) zeroL where stepR lastL accR accInitL = accR (stepL accInitL lastL) Somebody else, Jef G, then did an excellent job by

foldl versus foldr behavior with infinite lists

不羁岁月 提交于 2019-11-26 04:29:42
问题 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 reversed.) However, it no longer stops processing infinite lists. I attempted to trace the function\'s execution as in Apocalisp\'s answer: myAny even [1..] foldl step

Difference between reduce and foldLeft/fold in functional programming (particularly Scala and Scala APIs)?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 03:48:14
问题 Why do Scala and frameworks like Spark and Scalding have both reduce and foldLeft ? So then what\'s the difference between reduce and fold ? 回答1: reduce vs foldLeft A big big difference, not mentioned in any other stackoverflow answer relating to this topic clearly, is that reduce should be given a commutative monoid , i.e. an operation that is both commutative and associative. This means the operation can be parallelized. This distinction is very important for Big Data / MPP / distributed