Skip to content

Instantly share code, notes, and snippets.

@dz1984
Created July 4, 2017 13:28
Show Gist options
  • Save dz1984/bf650bca03d49f441506a8ba36c102ce to your computer and use it in GitHub Desktop.
Save dz1984/bf650bca03d49f441506a8ba36c102ce to your computer and use it in GitHub Desktop.
Using Racket to implement operators in the lambda calculus for Church number.
#lang racket
(define zero
(lambda (f)
(lambda (x) x)))
(define (add-1 n)
(lambda (f)
(lambda(x) (f ((n f) x)))))
(define one
(lambda (f)
(lambda (x) (f x))))
(define two
(lambda (f)
(lambda (x) (f (f x)))))
(define three
(lambda (f)
(lambda (x) (f (f (f x))))))
(define ((plus m) n)
(lambda (f)
(lambda (x)
((n f) ((m f) x)))))
(define (display-church-number n)
(display "λfλx.")
(display ((n (lambda (x) (cons "f" x))) "x"))
(newline))
(display-church-number zero)
(display-church-number one)
(display-church-number (add-1 zero))
(display-church-number two)
(display-church-number (add-1 one))
(display-church-number three)
(display-church-number (add-1 two))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment