Questions tagged [y-combinator]

The Y combinator is a higher-order function that allows a function that does not know its own name to call itself. It is the fundamental basis of recursion.

The Y combinator is a higher-order function that allows a function that does not know its own name to call itself. It is the fundamental basis of recursion.

In Scheme syntax:

  (define Y
    (lambda (f)
      (f (lambda (x) ((Y f) x)))))

To implement Fibonacci recursively with Y:

(define fibonacci 
  (Y
    (lambda (f)
      (lambda (n)
        (if (= n 0)
            1
            (* n (f (- n 1))))))) ))

A nice article on the topic is at: http://mvanier.livejournal.com/2897.html

86 questions
6
votes
1 answer

Two-layer "Y-style" combinator. Is this common? Does this have an official name?

I've been looking into how languages that forbid use-before-def and don't have mutable cells (no set! or setq) can nonetheless provide recursion. I of course ran across the (famous? infamous?) Y combinator and friends,…
danfuzz
  • 4,123
  • 20
  • 34
6
votes
3 answers

Fixed-Point Combinators

I am new to the world of fixed-point combinators and I guess they are used to recurse on anonymous lambdas, but I haven't really got to use them, or even been able to wrap my head around them completely. I have seen the example in Javascript for a…
5
votes
1 answer

Scala: (Int, Int) => Int doesn't match (Int, Int) => Int

I'm trying to use the y-combinator to define gcd in scala: object Main { def y[A,B]( f : (A => B) => A => B ) : A => B = f(y(f)) def gcd = y[(Int,Int),Int]( (g) => (x,y) => if (x == 0) y else g(y % x, x) ) } But I'm getting an…
rampion
  • 82,104
  • 41
  • 185
  • 301
5
votes
4 answers

Recursive Functions, Stack Overflows, and Y-Combinators

I have a recursive function (in C#) that I need to call about 800 million times; this would obviously normally result in a stack overflow after about the 900th call. I've kicked this out to multiple loops, but the recursive pattern is just so much…
5
votes
2 answers

Why does Visual Studio compile this function correctly without optimisation, but incorrectly with optimisation?

I'm doing some experimenting with y-combinator-like lambda wrapping (although they're not actually strictly-speaking y-combinators, I know), and I've encountered a very odd problem. My code operates exactly as I'd anticipate in a Debug configuration…
Stefan Bauer
  • 373
  • 1
  • 9
5
votes
5 answers

Does "Anonymous Recursion" work in .NET? It does in Mono

I surfed into this site a few days ago on "Anonymous Recursion in C#". The thrust of the article is that the following code will not work in C#: Func fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n; The article then goes into some detail…
Justin
  • 8,323
  • 4
  • 35
  • 41
5
votes
1 answer

How to get caching with the Y-combinator for this function

I have a coins = [200; 100; 50; 20; 10; 5; 2; 1] list and this recursive function to compute how many ways there are to give a certain amount of change (Spoiler alert for Project Euler problem 31): let rec f acc coins amount = if amount < 0…
primfaktor
  • 2,569
  • 23
  • 33
5
votes
2 answers

Y Combinator in Scheme using Define

In order to learn what a fixed-point combinator is and is used for, I wrote my own. But instead of writing it with strictly anonymous functions, like Wikipedia's example, I just used define: (define combine (lambda (functional) …
AlcubierreDrive
  • 3,506
  • 2
  • 22
  • 44
5
votes
1 answer

Y Combinator implementation Scheme

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…
Qandeel Abbasi
  • 929
  • 6
  • 29
5
votes
2 answers

Applying the Y-Combinator to a recursive function with two arguments in Clojure?

Doing the Y-Combinator for a single argument function such as factorial or fibonacci in Clojure is well documented: http://rosettacode.org/wiki/Y_combinator#Clojure My question is - how do you do it for a two argument function such as this getter…
hawkeye
  • 31,052
  • 27
  • 133
  • 271
4
votes
2 answers

Does Python have NO need for the Y-Combinator?

After an hour of trying to understand the Y-Combinator... i finally got it, mostly but then i realized that the same thing can be achieved without it... although I'm not sure if i fully understand it's purpose. eg. Factorials with Y-Combinator print…
user1218995
4
votes
2 answers

How to define Fibonacci function from non-recursive version?

I'm learning C++. As an exercise to myself, I'm trying to define the Fibonacci function from a non-recursive version using a Y combinator. In F# (or C#) I'd do it like this: let rec Y f n = f (Y f) n let protoFib f x = if n > 1 then f(n-1) + f(n-2)…
Colonel Panic
  • 119,181
  • 74
  • 363
  • 435
4
votes
2 answers

Why do I need to specify a return value for a function I'm passing to a Y combinator

I wrote a Y combinator like such: template struct Y{ F f; Y(F _f) : f{_f} {} template auto operator()(arg_t&&...arg) {return f(*this,std::forward(arg)...);} }; It works, but when I tried to define a factorial…
4
votes
1 answer

factorial function for Church numerals

I'm trying to implement the factorial lambda expression as described in the book Lambda-calculus, Combinators and Functional Programming The way it's described there is : fact = (Y)λf.λn.(((is-zero)n)one)((multiply)n)(f)(predecessor)n Y =…
4
votes
2 answers

fixed point combinator in lisp

;; 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…
alinsoar
  • 13,197
  • 4
  • 45
  • 63