#| Sigma : list-of-numbers -> number Purpose: add the nunbers in alon (define (Sigma alon) ... ) Examples: (Sigma (list 1 2 3)) = 6 (Sigma (list 10 2 13)) = 25 |# |
#| Pi : list-of-numbers -> number Purpose: multiply the nunbers in alon (define (Pi alon) ... ) Examples: (Pi (list 1 2 3)) = 6 (Sigma (list 10 2 13)) = 260 |# |
(define (Sigma alon) (cond ((empty? alon) 0) (else (+ (first alon) (Sigma (rest alon)))))) |
(define (Pi alon) (cond ((empty? alon) 1) (else (* (first alon) (Pi (rest alon)))))) |
Now abstract over the two: | |
(define (make-pi/sigma base combine) (local ((define (F alon) (cond ((empty? alon) base) (else (combine (first alon) (F (rest alon))))))) F)) | |
How do we get back Sigma and Pi: | |
(define Sigma (make-pi/sigma 0 +)) |
(define Pi (make-pi/sigma 1 *)) |
Let's verify that Pi and Sigma are defined properly.
(make-pi/sigma 1 *) = (local ((define (F alon) (cond ((empty? alon) base) (else (combine (first alon) (F (rest alon))))))) F) = F with (define (F alon) (cond ((empty? alon) base) (else (combine (first alon) (F (rest alon)))))) added to the Definitions window ;; So: (define (F alon) (cond ((empty? alon) base) (else (combine (first alon) (F (rest alon)))))) (define Pi F)
Now do this for Sigma and we get the following Definitions Window:
(define (F alon) (cond ((empty? alon) 0) (else (+ (first alon) (F (rest alon)))))) (define (F alon) (cond ((empty? alon) 1) (else (* (first alon) (F (rest alon)))))) (define Sigma F) (define Pi F)
Which one is it?
Before we lift local-definitions to the Definitions Window, we systematically rename them -- according to the laws of bindings and scope
Modify above derivations slightly!
(make-pi/sigma 1 *) = (local ((define (F alon) (cond ((empty? alon) base) (else (combine (first alon) (F (rest alon))))))) F) = (local ((define (FPI alon) (cond ((empty? alon) base) (else (combine (first alon) (FPI (rest alon))))))) FPI) because FPI does not occur at all in define's = FPI with (define (FPI alon) (cond ((empty? alon) base) (else (combine (first alon) (FPI (rest alon)))))) added to the Definitions window ;; So now we have: (define (FPI alon) (cond ((empty? alon) base) (else (combine (first alon) (FPI (rest alon)))))) (define Pi F) ;; in the Definition's window.
Matthias Felleisen | This page was generated on Fri Apr 9 09:17:38 CDT 1999. |