y-combinator

How do I define y-combinator without “let rec”?

独自空忆成欢 提交于 2019-11-27 00:12:13
问题 In almost all examples, a y-combinator in ML-type languages is written like this: let rec y f x = f (y f) x let factorial = y (fun f -> function 0 -> 1 | n -> n * f(n - 1)) This works as expected, but it feels like cheating to define the y-combinator using let rec ... . I want to define this combinator without using recursion, using the standard definition: Y = λf·(λx·f (x x)) (λx·f (x x)) A direct translation is as follows: let y = fun f -> (fun x -> f (x x)) (fun x -> f (x x));; However, F#

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)

Can a lambda function call itself recursively in Python?

可紊 提交于 2019-11-26 19:58:40
A regular function can contain a call to itself in its definition, no problem. I can't figure out how to do it with a lambda function though for the simple reason that the lambda function has no name to refer back to. Is there a way to do it? How? The only way I can think of to do this amounts to giving the function a name: fact = lambda x: 1 if x == 0 else x * fact(x-1) or alternately, for earlier versions of python: fact = lambda x: x == 0 and 1 or x * fact(x-1) Update : using the ideas from the other answers, I was able to wedge the factorial function into a single unnamed lambda: >>> map

Can a lambda function call itself recursively in Python?

本小妞迷上赌 提交于 2019-11-26 12:16:27
问题 A regular function can contain a call to itself in its definition, no problem. I can\'t figure out how to do it with a lambda function though for the simple reason that the lambda function has no name to refer back to. Is there a way to do it? How? 回答1: The only way I can think of to do this amounts to giving the function a name: fact = lambda x: 1 if x == 0 else x * fact(x-1) or alternately, for earlier versions of python: fact = lambda x: x == 0 and 1 or x * fact(x-1) Update : using the

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