fold

How do you know when to use fold-left and when to use fold-right?

血红的双手。 提交于 2019-11-27 04:01:54
问题 I'm aware that fold-left produces left-leaning trees and fold-right produces right-leaning trees, but when I reach for a fold, I sometimes find myself getting bogged down in headache-inducing thought trying to determine which kind of fold is appropriate. I usually end up unwinding the entire problem and stepping through the implementation of the fold function as it applies to my problem. So what I want to know is: What are some rules of thumb for determining whether to fold left or fold right

Difference between fold and foldLeft or foldRight?

安稳与你 提交于 2019-11-27 03:11:47
NOTE: I am on Scala 2.8—can that be a problem? Why can't I use the fold function the same way as foldLeft or foldRight ? In the Set scaladoc it says that: The result of folding may only be a supertype of this parallel collection's type parameter T . But I see no type parameter T in the function signature: def fold [A1 >: A] (z: A1)(op: (A1, A1) ⇒ A1): A1 What is the difference between the foldLeft-Right and fold , and how do I use the latter? EDIT: For example how would I write a fold to add all elements in a list? With foldLeft it would be: val foo = List(1, 2, 3) foo.foldLeft(0)(_ + _) //

Functional programming, Scala map and fold left [closed]

折月煮酒 提交于 2019-11-27 02:26:15
What are some good tutorials on fold left? Original question, restored from deletion to provide context for other answers: I am trying to implement a method for finding the boudning box of rectangle, circle, location and the group which all extends Shape. Group is basically an array of Shapes abstract class Shape case class Rectangle(width: Int, height: Int) extends Shape case class Location(x: Int, y: Int, shape: Shape) extends Shape case class Circle(radius: Int) extends Shape case class Group(shape: Shape*) extends Shape I got the bounding box computed for all three except the Group one. So

Implement zip using foldr

拟墨画扇 提交于 2019-11-27 02:08:09
问题 I'm currently on chapter 4 of Real World Haskell, and I'm trying to wrap my head around implementing foldl in terms of foldr. (Here's their code:) 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) I thought I'd try to implement zip using the same technique, but I don't seem to be making any progress. Is it even possible? 回答1: zip2 xs ys = foldr step done xs ys where done ys = [] step x zipsfn [] = [] step x zipsfn (y:ys) = (x, y) :

Left and Right Folding over an Infinite list

泄露秘密 提交于 2019-11-27 00:02:37
问题 I have issues with the following passage from Learn You A Haskell (Great book imo, not dissing it): One big difference is that right folds work on infinite lists, whereas left ones don't! To put it plainly, if you take an infinite list at some point and you fold it up from the right, you'll eventually reach the beginning of the list. However, if you take an infinite list at a point and you try to fold it up from the left, you'll never reach an end! I just don't get this. If you take an

What constitutes a fold for types other than list?

你说的曾经没有我的故事 提交于 2019-11-26 23:22:15
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 Notice that this reduction has a quite different character; whereas the list-based one is inherently

Infinite type error when defining zip with foldr only; can it be fixed?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 23:04:57
问题 (for the context to this see this recent SO entry). I tried to come up with the definition of zip using foldr only: zipp :: [a] -> [b] -> [(a,b)] zipp xs ys = zip1 xs (zip2 ys) where -- zip1 :: [a] -> tq -> [(a,b)] -- zip1 xs :: tr ~ tq -> [(a,b)] zip1 xs q = foldr (\ x r q -> q x r ) n xs q -------- c -------- n q = [] -- zip2 :: [b] -> a -> tr -> [(a,b)] -- zip2 ys :: tq ~ a -> tr -> [(a,b)] zip2 ys x r = foldr (\ y q x r -> (x,y) : r q ) m ys x r ---------- k -------------- m x r = [] {-

How does foldr work?

一世执手 提交于 2019-11-26 22:30:36
问题 Can anybody explain how does foldr work? 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? 回答1: foldr begins at the right-hand end of the list and combines each list entry with the accumulator value using the function you give it. The result is the final value of the accumulator after "folding" in all the list elements. Its type is: foldr :: (a -> b -> b) -> b -> [a] -> b and

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

帅比萌擦擦* 提交于 2019-11-26 21:28:18
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 clear that this example is trivial, but I am confused as to why foldr is beating foldl. Shouldn't this be

Reduce, fold or scan (Left/Right)?

做~自己de王妃 提交于 2019-11-26 21:08:37
When should I use reduceLeft , reduceRight , foldLeft , foldRight , scanLeft or scanRight ? I want an intuition/overview of their differences - possibly with some simple examples. 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 cumulate a single result using a start value. scanLeft and scanRight cumulate a collection of intermediate