Created
May 23, 2014 09:07
-
-
Save Liudx1985/3bfe5406974c89226d8c to your computer and use it in GitHub Desktop.
Y combinator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
; applicative-order Y-combinator. | |
; Y: The function that takes a function f and returns f (f (f (f (···)))) | |
(define Y | |
(lambda (le) | |
((lambda (f) (f f)) | |
(lambda (f) | |
(le (lambda (x) ((f f) x))))))) | |
(define length | |
(Y (lambda (length) | |
(lambda (l) | |
(cond ((null? l) 0) | |
(else | |
(add1 (length (cdr l))))))))) | |
(define fib | |
(Y (lambda (fib) | |
(lambda (n) | |
(cond ((eq? 0 n) 1) | |
((eq? 1 n) 1) | |
(else (+ (fib (- n 1)) (fib (- n 2))))))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment