y-combinator

Fixed point combinators in C++

强颜欢笑 提交于 2019-12-02 17:45:37
I'm interested in actual examples of using fixed point combinators (such as the y-combinator in C++. Have you ever used a fixed point combinator with egg or bind in real live code? I found this example in egg a little dense: void egg_example() { using bll::_1; using bll::_2; int r = fix2( bll::ret<int>( // \(f,a) -> a == 0 ? 1 : a * f(a-1) bll::if_then_else_return( _2 == 0, 1, _2 * lazy(_1)(_2 - 1) ) ) ) (5); BOOST_CHECK(r == 5*4*3*2*1); } Can you explain how this all works? Is there a nice simple example perhaps using bind with perhaps fewer dependancies than this one? Ted Here is the same

How to do this length≤1 more than once?

大憨熊 提交于 2019-12-02 03:22:39
问题 I've spent a day reading page 166's length≤1 in the book The Little Schemer ; there's the following code: (((lambda (mk-length) (mk-length mk-length)) (lambda (mk-length) (lambda (l) (cond ((null? l) 0) (else (add1 ((mk-length eternity) (cdr l)))))))) l) where l is (apples) and eternity is as follows: (define eternity (lambda (x) (eternity x))) Page 166 (4th ed.) states that: When we apply mk-length once, we get length≤1 And then Could we do this more than once? But I do not know how to do

Sharing vs. non-sharing fixed-point combinator

删除回忆录丶 提交于 2019-11-30 09:27:51
This is the usual definition of the fixed-point combinator in Haskell: fix :: (a -> a) -> a fix f = let x = f x in x On https://wiki.haskell.org/Prime_numbers , they define a different fixed-point combinator: _Y :: (t -> t) -> t _Y g = g (_Y g) -- multistage, non-sharing, g (g (g (g ...))) -- g (let x = g x in x) -- two g stages, sharing _Y is a non-sharing fixpoint combinator, here arranging for a recursive "telescoping" multistage primes production (a tower of producers). What exactly does this mean? What is "sharing" vs. "non-sharing" in that context? How does _Y differ from fix ? "Sharing"

Sharing vs. non-sharing fixed-point combinator

荒凉一梦 提交于 2019-11-29 14:29:24
问题 This is the usual definition of the fixed-point combinator in Haskell: fix :: (a -> a) -> a fix f = let x = f x in x On https://wiki.haskell.org/Prime_numbers, they define a different fixed-point combinator: _Y :: (t -> t) -> t _Y g = g (_Y g) -- multistage, non-sharing, g (g (g (g ...))) -- g (let x = g x in x) -- two g stages, sharing _Y is a non-sharing fixpoint combinator, here arranging for a recursive "telescoping" multistage primes production (a tower of producers). What exactly does

Have I implemented Y-combinator using C# dynamic, and if I haven't, what is it?

老子叫甜甜 提交于 2019-11-28 23:28:09
My brain seems to be in masochistic mode, so after being drowned in this , this and this , it wanted to mess around with some DIY in C#. I came up with the following, which I don't think is the Y-combinator, but it does seem to manage to make a non-recursive function recursive, without referring to itself: Func<Func<dynamic, dynamic>, Func<dynamic, dynamic>> Y = x => x(x); So given these: Func<dynamic, Func<dynamic, dynamic>> fact = self => n => n == 0 ? 1 : n * self(self)(n - 1); Func<dynamic, Func<dynamic, dynamic>> fib = self => n => n < 2 ? n : self(self)(n-1) + self(self)(n-2); We can

Y-Combinator Practical Example

一个人想着一个人 提交于 2019-11-28 15:40:07
问题 I've been reading a bit lately about functional programming and I am trying to grok the Y-Combinator. I understand that you can use the Y-Combinator to effectively implement recursion in a language that doesn't support recursion directly. However, every language that I'm likely to use already supports recursion so I'm not sure how useful it would be to use the Y-Combinator for that. Is there a better practical example of Y-Combinator usage that I'm missing? Has anyone actually used one in

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

て烟熏妆下的殇ゞ 提交于 2019-11-28 03:39:35
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# complains that it can't figure out the types: let y = fun f -> (fun x -> f (x x)) (fun x -> f (x x));;

Y Combinator in Haskell

半腔热情 提交于 2019-11-28 03:23:19
Is it possible to write the Y Combinator in Haskell? It seems like it would have an infinitely recursive type. Y :: f -> b -> c where f :: (f -> b -> c) or something. Even a simple slightly factored factorial factMaker _ 0 = 1 factMaker fn n = n * ((fn fn) (n -1) {- to be called as (factMaker factMaker) 5 -} fails with "Occurs check: cannot construct the infinite type: t = t -> t2 -> t1" (The Y combinator looks like this (define Y (lambda (X) ((lambda (procedure) (X (lambda (arg) ((procedure procedure) arg)))) (lambda (procedure) (X (lambda (arg) ((procedure procedure) arg))))))) in scheme) Or

I couldn't understand the Y-Combinator, so I tried to implement it and ended up with something shorter, which worked. How is that possible?

夙愿已清 提交于 2019-11-27 17:35:37
问题 I couldn't understand the Y-combinator, so I tried to implement a function that enabled recursion without native implementation. After some thinking, I ended up with this: Y = λx.(λv.(x x) v) Which is shorter than the actual one: Y = λf.(λx.f (x x)) (λx.f (x x)) And, for my surprise, worked. Some examples: // JavaScript Y = function(x){ return function(v){ return x(x, v); }; }; sum = Y(function(f, n){ return n == 0 ? 0 : n + f(f, n - 1); }); sum(4); ; Scheme (define Y (lambda (x) (lambda (v)

Y Combinator in Haskell

不羁岁月 提交于 2019-11-27 05:06:39
问题 Is it possible to write the Y Combinator in Haskell? It seems like it would have an infinitely recursive type. Y :: f -> b -> c where f :: (f -> b -> c) or something. Even a simple slightly factored factorial factMaker _ 0 = 1 factMaker fn n = n * ((fn fn) (n -1) {- to be called as (factMaker factMaker) 5 -} fails with "Occurs check: cannot construct the infinite type: t = t -> t2 -> t1" (The Y combinator looks like this (define Y (lambda (X) ((lambda (procedure) (X (lambda (arg) ((procedure