[Prev][Next][Index][Thread]
Re: small Q
David Rush <kumo@bellsouth.net> writes:
{stuff deleted}
> But we're talking (to some degree) about a 'turtles all the way down'
> solution, no? in a lambda+application *only* system, you'd use church
> numerals and then through the magic of combinators everything falls
> out again.
Natural numbers and cons for those who haven't had the joy of hacking the
pure lambda calculus...
A fully compliant RSR5 Scheme built only with lambda we can leave as an
exercise to the readers. :)
(define zero (lambda (z s) z))
(define succ (lambda (n)
(lambda (z s) (s (n z s)))))
(define one (succ zero))
(define nat_case (lambda (n z s) (n z s)))
(define if0 (lambda (n t f) ((nat_case n t
(lambda (x) f)))))
(define my-cons (lambda (fst snd) (lambda (f) (f fst snd))))
(define my-car (lambda (cell) (cell (lambda (fst snd) fst))))
(define my-cdr (lambda (cell) (cell (lambda (fst snd) snd))))
;; some examples
(if0 zero (lambda () (display "zero"))
(lambda () (display "not zero")))
(newline)
(if0 one (lambda () (display "zero"))
(lambda () (display "not zero")))
(newline)
(display (my-car (my-car (my-cons (my-cons 1 2) (my-cons 3 4))))) (newline)
(display (my-cdr (my-car (my-cons (my-cons 1 2) (my-cons 3 4))))) (newline)
(display (my-car (my-cdr (my-cons (my-cons 1 2) (my-cons 3 4))))) (newline)
(display (my-cdr (my-cdr (my-cons (my-cons 1 2) (my-cons 3 4))))) (newline)
(if0 (my-car (my-cons zero one)) (lambda () (display "zero"))
(lambda () (display "not zero")))
(newline)
(if0 (my-cdr (my-cons zero one)) (lambda () (display "zero"))
(lambda () (display "not zero")))
(newline)
Follow-Ups:
References: