fold

How to use foldr correctly in Haskell?

六眼飞鱼酱① 提交于 2020-01-04 23:05:30
问题 I'm trying to write a function which behave like this: correctCards :: [Card] -> [Card] -> Int It takes two lists of type Card and check for how many cards are the same. Here is my code: correctCards answer guess = foldr step acc guess where acc = 0 step acc guess | elem (head guess) answer = acc + 1 | otherwise = acc But the types are not match. Can someone tell me where I went wrong? Thanks. 回答1: Have a look at foldr 's type: foldr :: (a -> b -> b) -> b -> [a] -> b Now, that means that the

F# functional style approach much slower

僤鯓⒐⒋嵵緔 提交于 2020-01-02 12:16:13
问题 Trying to learn F#, by solving some programming puzzles. I don't want to add too many details about the problem as I don't want to spoil the fun for others. Basically, the issue is to find all 4-uples { (i,j,k,l) | i ^ j ^ k ^ l != 0 } with no repetition (eg., (1,1,1,2) and (1,1,2,1) are the same and should be counted just once). I have found a O(n^3) approach which works, please see countImperative(a,b,c,d) below. But I also tried to refactor the code as to get rid of the nested for loops.

Does Haskell have foldlM'?

心不动则不痛 提交于 2020-01-02 02:02:24
问题 How does one fold over a monad strictly? Data.Foldable has the strict foldl' and the monadic foldlM , but no strict foldlM' ? Is the strictness somehow defined by the monad itself? If so, how does one work out what it is? Imagine I must determine whether the product of an enormous list of ring elements is zero, but my ring isn't an integral domain, i.e. it contains zero devisors. In this case, I should tail recursively foldl my multiplication *** over the list, but return False the moment the

reduce() vs. fold() in Apache Spark

不问归期 提交于 2020-01-01 09:54:07
问题 What is the difference between reduce vs. fold with respect to their technical implementation? I understand that they differ by their signature as fold accepts additional parameter (i.e. initial value) which gets added to each partition output. Can someone tell about use case for these two actions? Which would perform better in which scenario consider 0 is used for fold ? Thanks in advance. 回答1: There is no practical difference when it comes to performance whatsoever: RDD.fold action is using

Haskell/GHC performance of `any`/`all`

妖精的绣舞 提交于 2019-12-31 00:56:45
问题 I wrote quantification functions exists , forall , and none for Haskell's build-in [] list data type. On multiple occasions, these seemed to prove much more efficient than Prelude / Data.List s any and all . I naively suspect that this performance is due to any and all being implemented using Θ(n) folds. Since I am relatively new to Haskell, I think I must be mistaken, or that there would be a good reason for this phenomenon. From Data.Foldable : -- | Determines whether any element of the

Why Haskell doesn't accept my combinatoric “zip” definition?

冷暖自知 提交于 2019-12-30 08:27:06
问题 This is the textbook zip function: zip :: [a] -> [a] -> [(a,a)] zip [] _ = [] zip _ [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys I asked on #haskell earlier wether "zip" could be implemented using "foldr" alone, no recursion, no pattern matching. After some thinking, we noticed the recursion could be eliminated using continuations: zip' :: [a] -> [a] -> [(a,a)] zip' = foldr cons nil where cons h t (y:ys) = (h,y) : (t ys) cons h t [] = [] nil = const [] We are still left with pattern matching

difference between foldLeft and reduceLeft in Scala

风流意气都作罢 提交于 2019-12-29 10:05:25
问题 I have learned the basic difference between foldLeft and reduceLeft foldLeft: initial value has to be passed reduceLeft: takes first element of the collection as initial value throws exception if collection is empty Is there any other difference ? Any specific reason to have two methods with similar functionality? 回答1: Few things to mention here, before giving the actual answer: Your question doesn't have anything to do with left , it's rather about the difference between reducing and folding

Scheme - sum the squares of even-valued elements in a list

十年热恋 提交于 2019-12-29 09:26:12
问题 I want to be able to sum the squares of the even elements in the list, however my current code only sums the elements, not the squares. Does anyone know of any modifications that can be made to make this to sum the squares of the even-valued elements in the list? (define (sum elemList) (if (null? elemList) 0 (+ (car elemList) (sum (cdr elemList))) ) ) My input would be: (sum-evens (list 1 2 3 4)) Output would be: 20 Which is (2*2) + (4*4) . If possible, it would be good to see both a

Explanation of lists:fold function

筅森魡賤 提交于 2019-12-29 09:05:28
问题 I learning more and more about Erlang language and have recently faced some problem. I read about foldl(Fun, Acc0, List) -> Acc1 function. I used learnyousomeerlang.com tutorial and there was an example (example is about Reverse Polish Notation Calculator in Erlang): %function that deletes all whitspaces and also execute rpn(L) when is_list(L) -> [Res] = lists:foldl(fun rpn/2, [], string:tokens(L," ")), Res. %function that converts string to integer or floating poitn value read(N) -> case

Explanation of lists:fold function

放肆的年华 提交于 2019-12-29 09:05:13
问题 I learning more and more about Erlang language and have recently faced some problem. I read about foldl(Fun, Acc0, List) -> Acc1 function. I used learnyousomeerlang.com tutorial and there was an example (example is about Reverse Polish Notation Calculator in Erlang): %function that deletes all whitspaces and also execute rpn(L) when is_list(L) -> [Res] = lists:foldl(fun rpn/2, [], string:tokens(L," ")), Res. %function that converts string to integer or floating poitn value read(N) -> case