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
10
votes
3 answers

Fixed point combinators in C++

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() { …
1800 INFORMATION
  • 119,313
  • 29
  • 152
  • 234
9
votes
3 answers

Alternative Y combinator definition

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…
Charles
  • 6,014
  • 6
  • 45
  • 66
9
votes
3 answers

Explain this implementation of the Y combinator in Scala?

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) => | …
liango
  • 706
  • 7
  • 17
8
votes
3 answers

Y combinator, Infinite types and Anonymous recursion in Haskell

I was trying to solve the maximal subsequence sum problem and came up with a neato solution msss :: (Ord a, Num a) => [a] -> a msss = f 0 0 f gmax _ [] = gmax f gmax lmax (x:xs) = let g = max (lmax + x) in f (g gmax) (g 0) xs You call the…
8
votes
4 answers

Y-combinator in D?

I'm trying to learn the Y-combinator better (I sort of understand it in Scheme) and implement it in D 2.0, and I'm failing pretty miserably: auto fact = delegate(uint delegate(uint) recurse) { return delegate(uint n) { return n > 1 ?…
user541686
  • 189,354
  • 112
  • 476
  • 821
8
votes
1 answer

Access outer variable inside a block and Y-combinator

I hope you all to be fine. I'm implementing the fixed-point Y-combinator in Harbour and I'm having some troubles with it. Well, the Y-combinator can be defined by the lambda-calculus as: Y = (λh.λF.F(λ x.((h(h))(F))(x))) (λh.λF.F(λ…
Marcelo Camargo
  • 2,041
  • 2
  • 17
  • 46
8
votes
2 answers

Defining a stack data structure and its main operations in lambda calculus

I'm trying to define a stack data structure in lambda calculus, using fixed point combinators. I am trying to define two operations, insertion and removal of elements, so, push and pop, but the only one I've been able to define, the insertion, is…
8
votes
3 answers

Y combinator: Some functions do not have fixed points

The Wikipedia article on the Y combinator provides the following JavaScript implementation of the Y combinator: function Y(f) { return ( (function (x) { return f(function (v) { return x(x)(v); }); }) (function (x) { …
Randomblue
  • 98,379
  • 133
  • 328
  • 526
7
votes
3 answers

Sharing vs. non-sharing fixed-point combinator

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) …
6
votes
1 answer

Expressing Y in term of SKI-Combinators in JavaScript

I was fiddling with Cominators in JavaScript and was being proud of (hopefully) getting S to work when I stumbled upon Wikipedia saying: "The Y combinator can be expressed in the SKI-calculus as: Y = S (K (S I I)) (S (S (K S) K) (K (S I I)))", so I…
6
votes
2 answers

Understanding the implementation of Y-Combinator

I would like to understand in mint detail please how we managed to get from the lambda calculus expression of Y-combinator : Y = λf.(λx.f (x x)) (λx.f (x x)) to the following implementation (in Scala) : def Y[A, B](f: (A => B) => A => B): A => B =…
6
votes
1 answer

y-combinator in StandardML

I know I can write the y-combinator in SML like this: First declare a new datatype to bypass the type mismatch due to circularity. datatype 'a mu = Roll of ('a mu -> 'a) val unroll = fn Roll x => x Now you can easily define the y-combinator: val Y…
NaCl
  • 2,525
  • 1
  • 19
  • 36
6
votes
1 answer

How to use Y- Combinator; why does this infinite recursion return 9?

Y - Combinator I've been trying to learn about Y - Combinators (an explanation on that would be lovely as well) and came across an example from this wiki. An in depth explanation on the subject would be much appreciated in either Haskell or…
6
votes
1 answer

Understanding Y Combinator through generic lambdas

While building a small lambda-based metaprogramming library, I had the necessity of using recursion in a C++14 generic lambda, to implement a left-fold. My own solution was passing the lambda itself as one of its parameters, like this: template…
Vittorio Romeo
  • 82,972
  • 25
  • 221
  • 369
6
votes
1 answer

Fixed point combinator usage? Why a stack overflow here?

I am confused about something. I wanted to generate an example (in Clojure) demonstrating how a fixed point combinator could be used to evaluate the fixed point of a sequence that mathematically converges after an infinite number of applications but…