Skip to content

Instantly share code, notes, and snippets.

@dvanhorn
Created October 10, 2011 03:17
Show Gist options
  • Save dvanhorn/1274565 to your computer and use it in GitHub Desktop.
Save dvanhorn/1274565 to your computer and use it in GitHub Desktop.
The straightforward way to write 256
#lang racket
;; Mogensen-Scott encoding
(define-syntax encode
(syntax-rules (λ)
[(encode (λ (x) e))
(λ (VAR) (λ (APP) (λ (LAM) (LAM (λ (x) (encode e))))))]
[(encode (e0 e1))
(λ (VAR) (λ (APP) (λ (LAM) ((APP (encode e0)) (encode e1)))))]
[(encode x)
(λ (VAR) (λ (APP) (λ (LAM) (VAR x))))]))
(define (decode t)
(((t (λ (x) x))
(λ (l)
(λ (r)
((decode l) (decode r)))))
(λ (f)
(λ (x) (decode (f x))))))
(define ev
(encode
((λ (f) ;; Y
((λ (r) (f (λ (a) ((r r) a))))
(λ (r) (f (λ (a) ((r r) a))))))
(λ (e) ;; eval
(λ (t)
(((t (λ (x) x))
(λ (l) (λ (r) ((e l) (e r)))))
(λ (f) (λ (x) (e (f x))))))))))
((((((decode ev) ev) ev)
(encode ((λ (s) (λ (z) (s (s z))))
((λ (s) (λ (z) (s (s z))))
((λ (s) (λ (z) (s (s z))))
(λ (s) (λ (z) (s (s z)))))))))
add1)
0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment