simple examples of higher-order procedures
procedure as argument
- (define (apply-either p q x) ((if (> x 0) p q) x))
- (define (abs x) (apply-either + - x))
- (define (abs-again x) (apply-either (lambda (i) i) - x))
procedure as result (1)
- (define (op b) (if b * +))
- ((op #t) 4 5) –> 9
procedure as result (2)
- (define (power k) (lambda (x) (exp x k))
- (define cube (power 3))
note
- both op and power return procedure values
- op selects its value from a finite, fixed repertoire:all the possible return values appear syntactically in its body
- power constructs a procedure on the fly:its repertoire of results is unbounded
- languages like C and Pascal allow you to write procedures like op, but not like power