combinators

Y combinator discussion in “The Little Schemer”

偶尔善良 提交于 2019-11-26 07:58:23
问题 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

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

感情迁移 提交于 2019-11-26 06:58:37
问题 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

foldl versus foldr behavior with infinite lists

不羁岁月 提交于 2019-11-26 04:29:42
问题 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

What is a Y-combinator? [closed]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 02:25:49
问题 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? 回答1: 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. 回答2: A Y