fold

Why is foldl defined in a strange way in Racket?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 12:34:37
In Haskell, like in many other functional languages, the function foldl is defined such that, for example, foldl (-) 0 [1,2,3,4] = -10 . This is OK, because foldl (-) 0 [1, 2,3,4] is, by definition, ((((0 - 1) - 2) - 3) - 4) . But, in Racket, (foldl - 0 '(1 2 3 4)) is 2, because Racket "intelligently" calculates like this: (4 - (3 - (2 - (1 - 0)))) , which indeed is 2. Of course, if we define auxiliary function flip, like this: (define (flip bin-fn) (lambda (x y) (bin-fn y x))) then we could in Racket achieve the same behavior as in Haskell: instead of (foldl - 0 '(1 2 3 4)) we can write:

iOS Paper fold (origami / accordion) effect animation, with manual control

混江龙づ霸主 提交于 2019-11-27 11:00:24
问题 I'm looking for tips on how to implement the popular 'paper folding / origami' effect in my iOS project. I'm aware of projects such as: https://github.com/xyfeng/XYOrigami but they only offer the 'animated' effect, with no manual control over the opening animation. I've struggled to dissect that project and come up with what I'm after. To be more exact, I'm looking on how to implement the effect shown here: http://vimeo.com/41495357 where the folding animation is not simply animated open, but

Difference between fold and reduce?

♀尐吖头ヾ 提交于 2019-11-27 10:55:23
问题 Trying to learn F# but got confused when trying to distinguish between fold and reduce. Fold seems to do the same thing but takes an extra parameter. Is there a legitimate reason for these two functions to exist or they are there to accommodate people with different backgrounds? (E.g.: String and string in C#) Here is code snippet copied from sample: let sumAList list = List.reduce (fun acc elem -> acc + elem) list let sumAFoldingList list = List.fold (fun acc elem -> acc + elem) 0 list

Why does this Haskell code work successfully with infinite lists?

可紊 提交于 2019-11-27 10:48:51
问题 I have some Haskell code that does work correctly on an infinite list, but I do not understand why it can do so successfully. (I modified my original code -- that did not handle infinite lists -- to incorporate something from some other code online, and suddenly I see that it works but don't know why). myAny :: (a -> Bool) -> [a] -> Bool myAny p list = foldr step False list where step item acc = p item || acc My understanding of foldr is that it will loop through every item in the list (and

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

淺唱寂寞╮ 提交于 2019-11-27 10:13:11
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? How can I quickly decide which type of fold to use given the problem I'm facing? There is an example

Implementing take using foldr

戏子无情 提交于 2019-11-27 07:55:15
问题 This is my take version using foldr : myTake n list = foldr step [] list where step x y | (length y) < n = x : y | otherwise = y main = do print $ myTake 2 [1,2,3,4] The output is not what I expect: [3,4] I then tried to debug by inserting the length of y into itself and the result was: [3,2,1,0] I don't understand why the lengths are inserted in decreasing order. Perhaps something obvious I missed? 回答1: If you want to implement take using foldr you need to simulate traversing the list from

right text align - bash

那年仲夏 提交于 2019-11-27 07:14:01
问题 I have one problem. My text should be aligned by right in specified width. I have managed to cut output to the desired size, but i have problem with putting everything on right side Here is what i got: #!/usr/local/bin/bash length=$1 file=$2 echo $1 echo -e "length = $length \t file = $file " f=`fold -w$length $file > output` while read line do echo "line is $line" done < "output" thanks 回答1: Try: printf "%40.40s\n" "$line" This will make it right-aligned with width 40. If you want no

How would you define map and filter using foldr in Haskell?

蹲街弑〆低调 提交于 2019-11-27 05:45:49
问题 I'm doing a bit of self study on functional languages (currently using Haskell). I came across a Haskell based assignment which requires defining map and filter in terms of foldr. For the life of me I'm not fully understanding how to go about this. For example when I define a map function like: map' :: (a -> b) -> [a] -> [b] map' f [] = [] map' f (x:xs) = foldr (\x xs -> (f x):xs) [] xs I don't know why the first element of the list is always ignored. Meaning that: map' (*2) [1,2,3,4] results

Using Haskell's map function to calculate the sum of a list

爷,独闯天下 提交于 2019-11-27 05:35:26
Haskell addm::[Int]->Int addm (x:xs) = sum(x:xs) I was able to achieve to get a sum of a list using sum function but is it possible to get the sum of a list using map function? Also what the use of map function? You can't really use map to sum up a list, because map treats each list element independently from the others. You can use map for example to increment each value in a list like in map (+1) [1,2,3,4] -- gives [2,3,4,5] Another way to implement your addm would be to use foldl : addm' = foldl (+) 0 Here it is, the supposedly impossible definition of sum in terms of map : sum' xs = let {

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

守給你的承諾、 提交于 2019-11-27 05:15:43
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... foldr can always be defined as: foldr f z t = appEndo (foldMap (Endo . f) t) z where appEndo and Endo are