#| 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 (F base combine alon) (cond ((empty? alon) base) (else (combine (first alon) (F base combine (rest alon)))))) | |
| How do we get back Sigma and Pi: | |
(define (Sigma alon) (F 0 + alon)) |
(define (Pi alon) (F 1 * alon)) |
;; smallest : alon [not empty] -> number ;; Purpose: finds smallest number in non-empty list (define (smallest alon) (F (first alon) min alon))
;; demands-going-up : number alon -> alon
;; Purpose: filters out all those n's in alon that are larger than number
(define (demands-going-up cf alon)
(local ((define (greater-than f rst)
(cond
((> f cf) (cons f rst))
(else rst))))
(F empty greater-than alon)))
;; project : db -> list-of-symbols
;; Purpose: it produces the names of those employees in a-db
;; who earn more than $1000000 (as a list)
(define (project a-db)
(local ((define (more-than-100000 f rst)
(cond
((> (pr-salary f) 1000000) (cons f rst))
(else rst))))
(F empty more-than-100000 a-db)))
Wow: F now consumes a list of personnel records, not just a list of
numbers. How can that be?
A list-of-numbers is either
- empty or
- (cons N L)
where N is a number
and L is a list-of-numbers.
|
A list-of-PRs is either
- empty or
- (cons N L)
where N is a PR
and L is a list-of-PRs.
|
| Now abstract over the two: | |
A list-of-XYZs is either - empty or - (cons N L) where N is a XYZ and L is a list-of-XYZs. | |
| Matthias Felleisen | This page was generated on Fri Apr 9 09:17:38 CDT 1999. |