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

List function with Y combinator does no recursion, why?

Note: This is kind of homework, kind of not -- the end goal is to have a function that produces a powerset of a set of numbers supplied to the function as a list of numbers. I have a recursive version of the function but I now need to find some ways…
David
  • 113
  • 2
  • 8
3
votes
1 answer

Fixed point of K combinator

The K combinator is K := (λxy.x) and the fixed point combinator is Y := λf.(λx.f x x) (λx.f x x). I tried to calculate YK: YK = (λx.Kxx)(λx.Kxx) = (λx.x)(λx.x) = (λx.x) = I So because YK is the fixed point of K: K(YK) = YK KI = I KIe = Ie = e for…
xuanji
  • 4,549
  • 2
  • 22
  • 33
3
votes
1 answer

Y combinator in elisp

We can define a recursive function, factorial as an example, by YCombinator as follows ;;; elisp ;;; This code works. Thanks to ;;; https://www.diegoberrocal.com/blog/2015/10/12/y-combinator-in-emacs-lisp/ (setq lexical-binding t) ;;; lexical ==…
Student
  • 404
  • 3
  • 9
3
votes
1 answer

Writing the Y combinator in typed/racket

Let's say I have an untyped implementation of the Y combinator in Racket. pasterack.org version #lang racket (define Y ((λ (f) (f f)) (λ (z) (λ (f) (f (λ (x) (((z z) f) x))))))) (define factorial (Y (λ…
Suzanne Soy
  • 2,522
  • 5
  • 32
  • 48
3
votes
2 answers

Little Schemer: write function that only supports lists of length ≤ 2

In the book The little schemer, we find this function that only supports lists with length smaller than or equal to 1: (((lambda (mk-length) ; A. (mk-length mk-length)) (lambda (mk-length) (lambda (l) …
3
votes
2 answers

YCombinator not working in Swift

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…
Encore PTL
  • 7,144
  • 8
  • 35
  • 72
3
votes
3 answers

Knights of the Lambda Calculus infinity written as lisp code

Knights of the Lambda Calculus logo have infinity written as (Y F) = (F (Y F)) is this lisp code the same and is it represent infinity too? (Y (λ (F) (Y F)))
jcubic
  • 51,975
  • 42
  • 183
  • 323
2
votes
2 answers

How do I manage declarations that require template parameters derived from recursive functors/lambdas?

I am attempting to build a clean and neat implementation of recursive-capable lambda self-scoping (which is basically a Y-combinator although I think technically not quite). It's a journey that's taken me to, among many others, this thread and this…
Stefan Bauer
  • 373
  • 1
  • 9
2
votes
1 answer

Why does this Y Combinator using code fail to compile?

I've been reading about combinators for three days now and I finally started writing them in code (more like copying stuff from places and making sense of things). Here's some code that I'm trying to run: #include #include…
Lost Arrow
  • 317
  • 1
  • 12
2
votes
3 answers

Weird error when using scoped type variables and the y combinator in haskell

So I'm playing around with the y-combinator and anonymous functions, and I ran into this weird error: Couldn't match expected type `t0 -> t1 -> t2' with actual type `forall b. b -> [b] -> [b]' The lambda expression `\ (n :: Int) newVal…
rampion
  • 82,104
  • 41
  • 185
  • 301
2
votes
3 answers

Why is the introduction of the Ycombinator in λ-calculus necessary?

I am reading a book on λ-calculus "Functional programming Through Lambda Calculus" (Greg Michaelson). In the book the author introduces a short-hand notation for defining functions. For example def identity = λx.x and goes on saying that we should…
user2465039
  • 814
  • 8
  • 22
2
votes
2 answers

Y-combinator in Scheme, using lazy evaluation?

Does anyone know how to implement the Y-combinator in Scheme, specifically with lazy evaluation and additional argument? It's my understanding that Scheme (promise?) (delay) and (force) provide lazy evaluation support. It's my understanding the…
user9405153
2
votes
2 answers

Finite number of recursions in Javascript with ES6 Y-combinator

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…
Louis Maddox
  • 4,311
  • 5
  • 28
  • 59
2
votes
2 answers

Little Schemer: why wrap (mk-length mk-length) into a function?

In The Little Schemer book, in Chapter 9, while building a length function for arbitrary long input, the following is suggested (on pages 170-171), that in the following code snippet (from page 168 itself): ((lambda (mk-length) (mk-length…
user8554766
2
votes
1 answer

How to use Kotlin to Write a Y-combinator function?

Can I use Kotlin FP (Lambda, function) to write a Y-combinator function? Y = λf.(λx.f (x x)) (λx.f (x x)) In JS: function Y(f) { return (function (g) { return g(g); })(function (g) { return f(function (x) { …
Jack Chen
  • 23
  • 2