y-combinator

Y Combinator implementation Scheme

╄→гoц情女王★ 提交于 2019-12-05 14:36:53
I am really new to scheme functional programming. I recently came across Y-combinator function in lambda calculus, something like this Y ≡ (λy.(λx.y(xx))(λx.y(xx))) . I wanted to implement it in scheme, i searched alot but i didn't find any implementation which exactly matches the above given structure. Some of them i found are given below: (define Y (lambda (X) ((lambda (procedure) (X (lambda (arg) ((procedure procedure) arg)))) (lambda (procedure) (X (lambda (arg) ((procedure procedure) arg))))))) and (define Y (lambda (r) ((lambda (f) (f f)) (lambda (y) (r (lambda (x) ((y y) x))))))) As you

fixed point combinator in lisp

无人久伴 提交于 2019-12-05 13:08:54
;; compute the max of a list of integers (define Y (lambda (w) ((lambda (f) (f f)) (lambda (f) (w (lambda (x) ((f f) x))))))) ((Y (lambda (max) (lambda (l) (cond ((null? l) -1) ((> (car l) (max (cdr l))) (car l)) (else (max (cdr l))))))) '(1 2 3 4 5)) I wish to understand this construction. Can somebody give a clear and simple explanation for this code? For example, supposing that I forget the formula of Y. How can I remember it , and reproduce it long after I work with it ? Will Ness Here's some related answers (by me): Y combinator discussion in "The Little Schemer" Unable to get

Fixed point combinator for mutually recursive functions?

对着背影说爱祢 提交于 2019-12-04 17:02:38
问题 Is there a fixed point combinator for creating tuples of mutually recursive functions? I.e. I'm looking for something like the Y-Combinator but which takes multiple "recursive"* functions, and will return a tuple of functions? *: not really recursive of course, as they are written to take themselves (and siblings) as arguments, in the usual Y-Combinator way. 回答1: The creature you are looking for is Y* combinator. Basing on this page by oleg-at-okmij.org I ported the Y* to Clojure: (defn Y* [&

Finite number of recursions in Javascript with ES6 Y-combinator

你说的曾经没有我的故事 提交于 2019-12-04 15:32:50
I came across an answer to another SO question about recursion in Javascript, that gave a very terse form in ES6 using the Y-combinator, using ES6's fat arrow, and thought hey, that'd be neat to use - then 15 minutes or so later had circled back to hm, maybe not . I've been to a few Haskell/Idris talks and run some code before, and familiar with standard JS, so was hoping I'd be able to figure this out, but can't quite see how a simple "do n recursions and return" is supposed to go, and where to implement a decrementing counter. I just wanted to simplify getting the n th parent node of a DOM

YCombinator not working in Swift

牧云@^-^@ 提交于 2019-12-03 17:18:03
I am trying to create a lambda function as such to get a factorial function but this throws a segmentation fault and errors out. How do I get this working in Swift. Please look at this video for reference on what I am trying to do http://www.confreaks.com/videos/1287-rubyconf2012-y-not-adventures-in-functional-programming typealias f = () -> () typealias g = (Int) -> (Int) typealias F = Any -> g let y = { (gen: Any) -> g in (gen as F)(gen) } let fact = y({ (gen: Any) -> g in { (n: Int) -> Int in if n == 0 { return 1 } else { return n * (gen as F)(gen)(n - 1) } } }) fact(10) There's a great

Alternative Y combinator definition

半世苍凉 提交于 2019-12-03 12:40:23
问题 I've spent some time wrapping my head around the Y combinator lately, and I've found that it is usually defined (more or less) as follows (this is in C#, but the language of choice isn't important): public delegate TResult SelfApplicable<TResult>(SelfApplicable<TResult> r); public static TResult U<TResult>(SelfApplicable<TResult> r) { return r(r); } public static Func<TArg1, TReturn> Y<TArg1, TReturn>(Func<Func<TArg1, TReturn>, Func<TArg1, TReturn>> f) { return U<Func<TArg1, TReturn>>(r =>

Using the Y Combinator in C#

馋奶兔 提交于 2019-12-03 05:43:49
问题 I'm trying to figure out how to write recursive functions (e.g. factorial, although my functions are much more complicated) in one line. To do this, I thought of using the Lambda Calculus' Y combinator. Here's the first definition: Y = λf.(λx.f(x x))(λx.f(x x)) Here's the reduced definition: Y g = g(Y g) I attempted to write them in C# like this: // Original Lambda Y = f => (new Lambda(x => f(x(x)))(new Lambda(x => f(x(x))))); // Reduced Lambda Y = null; Y = g => g(Y(g)); ( Lambda is a Func

Fixed point combinators in C++

假如想象 提交于 2019-12-03 04:35:12
问题 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

Explain this implementation of the Y combinator in Scala?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 00:19:55
This is a implementation of the Y-combinator in Scala: scala> def Y[T](func: (T => T) => (T => T)): (T => T) = func(Y(func))(_:T) Y: [T](func: (T => T) => (T => T))T => T scala> def fact = Y { | f: (Int => Int) => | n: Int => | if(n <= 0) 1 | else n * f(n - 1)} fact: Int => Int scala> println(fact(5)) 120 Q1: How does the result 120 come out, step by step? Because the Y(func) is defined as func(Y(func)) , the Y should become more and more,Where is the Y gone lost and how is the 120 come out in the peform process? Q2: What is the difference between def Y[T](func: (T => T) => (T => T)): (T => T)

Using the Y Combinator in C#

旧街凉风 提交于 2019-12-02 19:03:19
I'm trying to figure out how to write recursive functions (e.g. factorial, although my functions are much more complicated) in one line. To do this, I thought of using the Lambda Calculus' Y combinator . Here's the first definition: Y = λf.(λx.f(x x))(λx.f(x x)) Here's the reduced definition: Y g = g(Y g) I attempted to write them in C# like this: // Original Lambda Y = f => (new Lambda(x => f(x(x)))(new Lambda(x => f(x(x))))); // Reduced Lambda Y = null; Y = g => g(Y(g)); ( Lambda is a Func<dynamic, dynamic> . I first tried to typedef it with using , but that didn't work , so now it's defined