March 26th Monday (三月  二十六日 月曜日)

来源:互联网 发布:网络交换器什么牌子好 编辑:程序博客网 时间:2024/04/19 21:49

(define (f x) (cons 'x x))

(define (zero f)
  (lambda (x)
    x))

(define (one f)
  (lambda (x)
    (f x)))

(define (two f)
  (lambda (x)
    (f (f x))))

(define (three f)
  (lambda (x)
    (f (f (f x)))))

(define (add-one n)
  (lambda (f)
    (lambda (x)
      (f ((n f) x)))))

(define (add a b)
  (lambda (f)
    (lambda (x)
      ((a f) ((b f) x)))))

(define (mul a b)
  (lambda (f) (lambda (x) ((a (b f)) x))))

  The above list are all Church Numbers.  Yes, it is so famous.  At the begin of learning them, I
was confused.  Having gong over the comprehension I found it is so simple.  I thought I know how
to Church invent those interesting numbers.  In fact, he just found it.

  Let's look at "one", "two", "three", and "zero".  As a matter of fact, those numbers all are
high order operators.  That is to say, they like "map", "for-each", they take a function and one
parameter.  What is the discrepancy between "map" and them?  Look inside of them!

  The "zero" is just return its argument and not call the function taken; the "one" just call its
function to its argument taken one time; the "two" call the function to its argument only two
times.  From them, we know a new way to counter -- composing a function with a parameter.

  ((two f) 'z) => (x x . 'z)

  So, the "add-one" is also simple.  Firstly, let's look at the above statement how to use "two".
The "(two f)" is the key to understand "add-one", "add".  The "((n f) x)" is alike the "((two f) 'z)".
Yes, it will compose "f" with "x".  Finally, compose "f" again.  In other words, call "f" with a result
returned from "((n f) x)".   Now the "add" is easy to understand!  Having composing "f" "b" times with "x",
call the result function from "(a f)" with result returned from "((b f) x)".

  Now we know it is so important for Church numbers or related operators to make a correct composite
function.  So, the "mul" is also not hard to understand.  The "(two f)" will make a composite function
like "(f (f (f ...)))".  The "(a (b f))" is mean to compose "f" "(a - 1) * (b - 1)" times.  For example,
if "a" is three and "b" is two, the number of composing "f" is (3-1) * (2-1) = 2.  "(f (f ...))" resulted
from "(two f)", "(p (p (p ...)" resulted from "(three p)".  Having substituted "(f (f ...))" for "p".  The
final composite result is "(f (f (f (f (f (f ...))))))".  OK, our goal reached at.