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

92 questions
86
votes
17 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
57
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
  • 26,048
  • 36
  • 112
  • 180
52
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,980
  • 2
  • 22
  • 33
49
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
  • 80,494
  • 45
  • 196
  • 228
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,242
  • 2
  • 19
  • 21
38
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
  • 2,047
  • 18
  • 18
24
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
  • 9,207
  • 3
  • 29
  • 57
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
  • 7,224
  • 4
  • 33
  • 56
19
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
  • 63,493
  • 27
  • 91
  • 122
17
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…
17
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
  • 51,090
  • 44
  • 144
  • 286
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
  • 62,528
  • 13
  • 153
  • 317
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
  • 63,995
  • 54
  • 186
  • 268
11
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
  • 131,367
  • 29
  • 160
  • 239
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,626
  • 2
  • 23
  • 40
1
2 3 4 5 6 7