; *zero* is a (higher-order) function "(lambda (f) (lambda (x) x)" that
; takes a function as the sole argument (f),
; and returns another function "(lambda (x) x)"
; which applies f on its sole argument (x) 0 times
; we can see x is actually a dummy...
(define zero
(lambda (f) (lambda (x) x)))
; we clearly see that, the approach is to represent numbers by functions
; if we apply *number* functions to a function f,
; we get a function that repeatly applies f to its argument *number* times.
; that is, (n f) evaluates to a function which repeatly applies f to its argument n times.
; so ((n f) x) is the result of repeatly applying f to x n times
; (f ((n f) x)) is the result of repeatly applying f to x (n + 1) times
; *add-1* is a (higher-order) function that
; takes a function (representing a number) as the sole argument (n),
; and returns another function (representing a number) "(lambda (f) (lambda (x) (f ((n f) x))))",
; which takes a function as the sole argument (f),
; and returns another function "(lambda (x) (f ((n f) x)))",
; which actually applies f on x (n + 1) times
; so *add-1* returns the function representing n + 1
(define (add-1 n)
(lambda (f) (lambda (x) (f ((n f) x)))))
; if we take the advice and use the substitution model...
; one is (add-1 zero)
(add-1 zero)
(add-1 (lambda (f) (lambda (x) x)))
(lambda (f) (lambda (x) (f (((lambda (f) (lambda (x) x)) f) x))))
(lambda (f) (lambda (x) (f ((lambda (x) x) x))))
(lambda (f) (lambda (x) (f x)))
; so one is...
(define one
(lambda (f) (lambda (x) (f x))))
; and two...
(define two
(lambda (f) (lambda (x) (f (f x)))))
; and plus...
(define (+ a b)
(lambda (f) (lambda (x) ((b f) ((a f) x)))))
/** * 練習2.2。 * 中点を求める。 */ struct point { int x; int y; } struct segment { point start; point end; } void printPoint(point p) { Stdout.formatln("({},{})", p.x, p.y); } point midpointSegment(segment s) { point p; p.x = (s.start.x + s.end.x) / 2; p.y = (s.start.y + s.end.y) / 2; return p; }