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
72
votes
14 answers

Can a lambda function call itself recursively in Python?

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? …
dsimard
55
votes
8 answers

Good explanation of "Combinators" (For non mathematicians)

Anyone got a good explanation of "combinators" (Y-combinators etc. and NOT the company)? I'm looking for one for the practical programmer who understands recursion and higher-order functions, but doesn't have a strong theory or math…
interstar
  • 22,620
  • 31
  • 101
  • 161
49
votes
5 answers

Y Combinator in Haskell

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…
Theo Belaire
  • 2,732
  • 1
  • 19
  • 31
47
votes
5 answers

How do I define y-combinator without "let rec"?

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…
Juliet
  • 76,873
  • 44
  • 191
  • 224
40
votes
5 answers

Y-Combinator Practical Example

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,…
onedozenbagels
  • 2,132
  • 2
  • 18
  • 21
35
votes
2 answers

Y combinator discussion in "The Little Schemer"

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…
planarian
  • 1,877
  • 16
  • 17
23
votes
1 answer

Using the Y Combinator in C#

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…
Jashaszun
  • 8,903
  • 3
  • 21
  • 51
20
votes
4 answers

Why is the type of this function (a -> a) -> a?

Why is the type of this function (a -> a) -> a? Prelude> let y f = f (y f) Prelude> :t y y :: (t -> t) -> t Shouldn't it be an infinite/recursive type? I was going to try and put into words what I think it's type should be, but I just can't do it…
TheIronKnuckle
  • 6,986
  • 3
  • 29
  • 51
20
votes
7 answers

Higher-order function of recursive functions?

Is there some way to "wrap" a recursive function via a higher-order function, so that the recursive call is also wrapped? (e.g. to log the arguments to the function on each call.) For example, suppose we have a function, sum(), that returns the sum…
mjs
  • 57,072
  • 26
  • 82
  • 114
16
votes
2 answers

Why inductive datatypes forbid types like `data Bad a = C (Bad a -> a)` where the type recursion occurs in front of ->?

Agda manual on Inductive Data Types and Pattern Matching states: To ensure normalisation, inductive occurrences must appear in strictly positive positions. For instance, the following datatype is not allowed: data Bad : Set where bad : (Bad →…
Petr
  • 60,177
  • 8
  • 136
  • 295
15
votes
2 answers

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?

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…
MaiaVictor
  • 45,122
  • 42
  • 127
  • 254
14
votes
1 answer

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

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…
Benjol
  • 57,639
  • 51
  • 180
  • 252
13
votes
3 answers

Fixed point combinator for mutually recursive functions?

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…
10
votes
2 answers

Transforming a function that computes a fixed point

I have a function which computes a fixed point in terms of iterate: equivalenceClosure :: (Ord a) => Relation a -> Relation a equivalenceClosure = fst . List.head -- "guaranteed" to exist . List.dropWhile (uncurry…
nomen
  • 3,428
  • 1
  • 21
  • 35
10
votes
4 answers

How does Y-combinator compute the fixed point programmatically?

I believe I understand mathematically the idea of Y-combinator: it returns the fixed point of a given functional F, thus f = Y(F) where f satisfies f == F(f). But I don't understand how it does the actual computation program wise? Let's take the…
hackape
  • 11,966
  • 1
  • 15
  • 40
1
2 3 4 5 6