6  Wednesday Afternoon: Using Abstractions

Related material in How to Design Programs: section 21.1

6.1  Goals: 1001 Loops

(1) to iterate: to apply an action to each item in a piece of compound data

(2) studying lambda: writing actions is easy with lambda

;; build-list :  N ( N  →  X)  →  (listof X)
;; to construct (list (f 0) ... (f (- n 1)))
(define (build-list n f) ...)

;; filter : (X  →  boolean) (listof X)  →  (listof X)
;; to construct a list from all those items on alox for which p holds 
(define (filter p alox) ...)

;; map : (X  →  Y) (listof X)  →  (listof Y)
;; to construct a list by applying f to each item on alox
;; that is, (map f (list x-1 ... x-n)) = (list (f x-1) ... (f x-n))
(define (map f alox) ...)

;; andmap : (X  →  boolean) (listof X)  →  boolean
;; to determine whether p holds for every item on alox
;; that is, (andmap p (list x-1 ... x-n)) = (and (p x-1) (and ... (p x-n)))
(define (andmap p alox) ...)

;; ormap : (X  →  boolean) (listof X)  →  boolean
;; to determine whether p holds for at least one item on alox
;; that is, (ormap p (list x-1 ... x-n)) = (or (p x-1) (or ... (p x-n)))
(define (ormap p alox) ...)

Figure 1:  Some of Scheme’s loops