fold

Example of the difference between List.fold and List.foldBack

爷,独闯天下 提交于 2019-12-29 07:27:30
问题 My understanding of the difference between List.fold and List.foldBack is that foldBack iterates over its list in a reverse order. Both functions accumulate a result from the items in the list. I'm having trouble coming up with a good example where it is preferable to foldBack over a list. In the examples I have come up with, the results are the same for both fold and foldBack, if the function logic does the same thing. [<Fact>] let ``List.foldBack accumulating a value from the right to the

Foldr/Foldl for free when Tree is implementing Foldable foldMap?

家住魔仙堡 提交于 2019-12-28 02:12:10
问题 I am a beginner at Haskell and learning from "Learn You a Haskell". There's something I don't understand about the Tree implementation of Foldable . instance F.Foldable Tree where foldMap f Empty = mempty foldMap f (Node x l r) = F.foldMap f l `mappend` f x `mappend` F.foldMap f r Quote from LYAH: "So if we just implement foldMap for some type, we get foldr and foldl on that type for free !" . Can someone explain this? I don't understand how and why do I get foldr and foldl for free now...

tree-fold defintion that works both with + and append

亡梦爱人 提交于 2019-12-25 01:09:22
问题 (define (tree-fold f tree) (if (pair? tree) (apply f (car tree) (map (lambda (t) (tree-fold f t)) (cdr tree))) (f tree))) works for example with: (tree-fold + '(1 (2 2)(2 2)) -> 9 However if I want to use (tree-fold append '(1 (2 2)(2 2))) , I have to modify the tree-fold with list around (car tree) , which breaks it for + . Is there some mechanism that can be used in the tree-fold definition that would make it work with both + and append ? 回答1: This should work, adding one parameter to

How do I combine two foldr/ foldl functions for this output? (Racket/Scheme)

99封情书 提交于 2019-12-24 19:24:25
问题 I am quite new to racket, and I wondered if there is a way to combine two foldr` functions: '(foldr + 0 (list 1 2 3 4)) ;; output = 10 -> (4+(3+(2+(1+0)))) (foldr * 1 (list 1 2 3 4)) ;; output = 24 -> (4*(3*(2*(1*0))))' I want to receive this output : output = 64 -> (4+4∗(3+3∗(2+2∗(1+1∗0)))) 回答1: #| (4*(3*(2*(1*0)))) -> 0 (not 24) (4+(3+(2+(1+0)))) -> 10 (foldr + 0 (list 1 2 3 4)) -> (+ 1 (+ 2 (+ 3 (+ 4 0)))) -> 10 (foldr * 1 (list 1 2 3 4)) -> (* 1 (* 2 (* 3 (* 4 1)))) -> 24 if you want this

Implement insert in haskell with foldr

ぃ、小莉子 提交于 2019-12-24 02:18:51
问题 How to implement insert using foldr in haskell. I tried: insert'' :: Ord a => a -> [a] -> [a] insert'' e xs = foldr (\x -> \y -> if x<y then x:y else y:x) [e] xs No dice. I have to insert element e in list so that it goes before first element that is larger or equal to it. Example: insert'' 2.5 [1,2,3] => [1.0,2.0,2.5,3.0] insert'' 2.5 [3,2,1] => [2.5,3.0,2.0,1.0] insert'' 2 [1,2,1] => [1,2,2,1] In last example first 2 is inserted one. EDIT: Thanks @Lee. I have this now: insert'' :: Ord a =>

Type variance error in Scala when doing a foldLeft over Traversable views

倾然丶 夕夏残阳落幕 提交于 2019-12-24 00:39:58
问题 I am trying concatenate a series of Traversable views in Scala using a foldLeft operator and am hitting type variance errors that I don't understand. I can use reduce to concatenate a list of Traversable views like so. val xs = List(1,2,3,4).map(Traversable(_).view).reduce((a: TraversableView[Int, Traversable[_]], b: TraversableView[Int, Traversable[_]]) => a ++ b) // TraversableView[Int,Traversable[_]] // xs.force returns Traversable[Int] = List(1, 2, 3, 4) (Note that I have to write the

How would I implement this fold function?

社会主义新天地 提交于 2019-12-23 15:25:23
问题 Given are the two datatypes Color and Plant. data Color = Red | Pink | White | Blue | Purple | Green | Yellow deriving (Show, Eq) data Plant = Leaf | Blossom Color | Stalk Plant Plant deriving (Show, Eq) Now I'm supposed to implement a function fold_plant of following type: (x -> x -> x) -> (Color -> x) -> x -> Plant -> x The way I understand a fold function is that it takes a list and for each iteration it removes the first element from the list and does something with that element.

Implementing static version of std::all_of using template metaprogramming?

孤街浪徒 提交于 2019-12-23 12:21:34
问题 Preface . I'm trying to get somewhat deeper understanding of C++ template metaprogramming and it seems, that I'm stuck... I'm writing a library, which we will use for binary data [de]serialization. The expected structure of data being unpacked is known to a certain extent and it seems reasonable for me to use this knowledge to (1) validate data (2) skip irrelevant parts and (3) unpack the data directly into structs known at compile-time - both for avoiding unnecessary copying and making the

F# - Breaking a List into Concatenated Strings by an Interval

那年仲夏 提交于 2019-12-23 03:44:13
问题 I have a list of email addresses, and I need to send an email notification to each address. I'd like to do this in blocks of 25 addresses at a time. Is there any quick way in a functional language like F# to "fold" (concatenate) 25 emails addresses together... each separated by a semicolon. I know there is the String.Split method in .NET, but I need to concat 25 at a time. What is the most elegant way to perform this in F#? 回答1: Here's a way to break into groups of at most N: // break a

Generalized foldr and foldl for using with generic Haskell trees?

陌路散爱 提交于 2019-12-23 02:50:19
问题 How can I write generalized foldr and foldl function for generic Haskell trees, given this definition? data (Eq a, Show a) => Tree a = Void | Node a [Tree a] deriving (Eq, Show) treefoldr :: (Eq a, Show a) => (a -> b -> c) -> c -> (c -> b -> b) -> b -> Tree a -> c treefoldl :: (Eq a, Show a) => (b -> a -> c) -> c -> (c -> b -> b) -> b -> Tree a -> c Even if I can understand how foldr and foldl functions work in Haskell, I'm not quite sure how to write this generalized function for trees. EDIT