Fundamentals of Computer Science I (CS151 2003F)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[Honesty]
[Instructions]
[Links]
[Guidelines for Lab Writeups]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Scheme Report]
[Glimmer Scheme Reference]
[CSC151.01 (Gum)]
[CSC151 2003S]
[CSC151 2002F]
[SamR]
Back to Procedures as Values (2). On to Procedures as Values (4).
Held: Thursday, 6 November 2003
Summary: We continue our discussion and expermentation with higherorder procedures.
Related Pages:
Assignments
Notes:
Overview:
function compositionoperation that Mathematicians love takes two functions as parameters and returns a new function.
odd?
were defined but even?
were not, we'd simply say that .even?
is the opposite ofodd?
longhandas
(define even? (lambda (val) (not (odd? val))))
(define even? (negate odd?))
negate
is a procedure that builds
procedures.
(define negate (lambda (pred?) (lambda (val) (not (pred? val)))))
lambda
in the definition of even?
.
negate
.
lambda
expressions in the definition
of negate
?
negate
. The other is the
parameter of the procedure returned by negate
.
odd?
is that it crashes and burns when
given a nonnumber. Hence, we might like to define an
oddnumber?
predicate that holds if its parameter
is both odd and a number.
(define oddnumber? (both number? odd?))
both
as
(define both (lambda (pred1? pred2?) (lambda (val) (and (pred1? val) (pred2? val)))))
either
as
(define either (lambda (pred1? pred2?) (lambda (val) (or (pred1? val) (pred2? val)))))
listp?
(define listp? (either null? (both pair? (compose listp? cdr))))
(define x (+ x 1))
lambda
does something special.
(lambda (lst) (cond ((null? lst) 0) ((pred? (car lst)) (+ 1 (recurse (cdr lst)))) (else (recurse (cdr lst)))))
define
)? We use letrec
!
(define makecounter (lambda (pred?) (letrec ((counter (lambda (lst) (cond ((null? lst) 0) ((pred? (car lst)) (+ 1 (counter (cdr lst)))) (else (counter (cdr lst))))))) counter)))
(define countodds (makecounter odd?)) (define countevens (makecounter even?))
countevens
that way
(define countevens (lambda (lst) (countevenshelper lst 0))) (define countevenshelper (lambda (lst count) (cond ((null? lst) count) ((even? (car lst)) (countevenshelper (cdr lst) (+ 1 count))) (else countevenshelper (cdr lst) count))))
makecounter
to use that strategy.
(define makecounter (lambda (pred?) (letrec ((counter (lambda (lst count) (cond ((null? lst) count) ((pred? (car lst)) (counter (cdr lst) (+ count 1))) (else (counter (cdr lst) count)))))) (lambda (lst) (counter lst 0)))))
Back to Procedures as Values (2). On to Procedures as Values (4).
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[Honesty]
[Instructions]
[Links]
[Guidelines for Lab Writeups]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Scheme Report]
[Glimmer Scheme Reference]
[CSC151.01 (Gum)]
[CSC151 2003S]
[CSC151 2002F]
[SamR]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Dec 9 13:59:53 2003.
The source to the document was last modified on Mon Sep 1 13:30:51 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Outlines/outline.37.html
.
You may wish to validate this document's HTML ; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu