Church计数

my code:

;Church计数

(define zero (lambda (f) (lambda (x) x)))
(define (add-1 n) (lambda (f) (lambda (x) (f ((n f) x)))))
(define (show-num n)((n (lambda (x) (+ 1 x))) 0))
(define (add a b)(lambda (f) (lambda (x) ((a f) ((b f) x)))))
(define one (lambda (f) (lambda (x) (f x))))
(define two (lambda (f) (lambda (x) (f (f x)))))
(show-num zero)
(show-num one)
(show-num two)
(newline)
(show-num (add-1 zero))
(show-num (add-1 (add-1 zero)))
(show-num (add-1 (add-1 (add-1 zero))))
(show-num (add-1 (add-1 (add-1 (add-1 zero)))))
(newline)
(display "my add\n")
(show-num (add zero zero))
(show-num (add (add-1 zero) (add-1 zero)))
(show-num (add (add-1 zero) (add-1 (add-1 zero))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Church计数,通过(lambda (f))把f作为参数,这样f就不会被求值,而f的执行次数就代表了数字。

lambda中的形参其值是未知的,所以不会被求值。

show-num用来把Church计数方式的数字转换成普通数字。

你可能感兴趣的:(旧代码)