combinators

Parallel map in haskell

╄→尐↘猪︶ㄣ 提交于 2019-11-28 18:38:01
Is there some substitute of map which evaluates the list in parallel? I don't need it to be lazy. Something like: pmap :: (a -> b) -> [a] -> [b] letting me pmap expensive_function big_list and have all my cores at 100%. Yes, see the parallel package : ls `using` parList rdeepseq will evaluate each element of the list in parallel via the rdeepseq strategy. Note the use of parListChunk with a good chunk value might give better performance if your elements are too cheap to get a benefit evaluating each one in parallel (because it saves on sparking for each element). EDIT: Based on your question I

Implement zip using foldr

北城余情 提交于 2019-11-28 09:03:22
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? zip2 xs ys = foldr step done xs ys where done ys = [] step x zipsfn [] = [] step x zipsfn (y:ys) = (x, y) : (zipsfn ys) How this works: (foldr step done xs) returns a function that consumes ys; so we go down the xs list

How does Data.MemoCombinators work?

北战南征 提交于 2019-11-27 03:13:08
I've been looking at the source for Data.MemoCombinators but I can't really see where the heart of it is. Please explain to me what the logic is behind all of these combinators and the mechanics of how they actually work to speed up your program in real world programming. I'm looking for specifics for this implementation, and optionally comparison/contrast with other Haskell approaches to memoization. I understand what memoization is and am not looking for a description of how it works in general. This library is a straightforward combinatorization of the well-known technique of memoization.

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) :

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

Y combinator discussion in “The Little Schemer”

佐手、 提交于 2019-11-26 21:33:12
So, I've spent a lot of time reading and re-reading the ending of chapter 9 in The Little Schemer , where the applicative Y combinator is developed for the length function. I think my confusion boils down to a single statement that contrasts two versions of length (before the combinator is factored out): A: ((lambda (mk-length) (mk-length mk-length)) (lambda (mk-length) (lambda (l) (cond ((null? l) 0 ) (else (add1 ((mk-length mk-length) (cdr l)))))))) B: ((lambda (mk-length) (mk-length mk-length)) (lambda (mk-length) ((lambda (length) (lambda (l) (cond ((null? l) 0) (else (add1 (length (cdr l)

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

foldl versus foldr behavior with infinite lists

帅比萌擦擦* 提交于 2019-11-26 15:36:47
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 False [1..] step (foldl step False [2..]) 1 even 1 || (foldl step False [2..]) False || (foldl step False

How does Data.MemoCombinators work?

ぃ、小莉子 提交于 2019-11-26 12:38:07
问题 I\'ve been looking at the source for Data.MemoCombinators but I can\'t really see where the heart of it is. Please explain to me what the logic is behind all of these combinators and the mechanics of how they actually work to speed up your program in real world programming. I\'m looking for specifics for this implementation, and optionally comparison/contrast with other Haskell approaches to memoization. I understand what memoization is and am not looking for a description of how it works in

What is a Y-combinator? [closed]

梦想与她 提交于 2019-11-26 11:27:16
A Y-combinator is a computer science concept from the “functional” side of things. Most programmers don't know much at all about combinators, if they've even heard about them. What is a Y-combinator? How do combinators work? What are they good for? Are they useful in procedural languages? Nicholas Mancuso If you're ready for a long read, Mike Vanier has a great explanation . Long story short, it allows you to implement recursion in a language that doesn't necessarily support it natively. Chris Ammerman A Y-combinator is a "functional" (a function that operates on other functions) that enables