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

[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Project]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Scheme Reference]
[Scheme Report]
[CS151 2003S Gum]
[CS151 2002F]
[CS151 History]
[SamR]
Back to Procedures as Values. On to Early Break.
Held: Wednesday, 12 March 2003
Summary: Today we continue our investigations into higherorder procedures by considering procedures that return procedures and by working on some handson activities.
Related Pages:
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? (oppositeof odd?))
oppositeof
is a procedure that builds
procedures.
(define oppositeof (lambda (pred?) (lambda (val) (not (pred? val)))))
lambda
in the definition of even?
.
oppositeof
.
lambda
expressions in the definition
of oppositeof
?
oppositeof
. The other is the
parameter of the procedure returned by oppositeof
.
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 odd? number?))
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)))))
Thursday, 16 January 2003 [Samuel A. Rebelsky]
Wednesday, 12 March 2003 [Samuel A. Rebelsky]
Back to Procedures as Values. On to Early Break.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Project]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Scheme Reference]
[Scheme Report]
[CS151 2003S Gum]
[CS151 2002F]
[CS151 History]
[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 May 6 09:30:02 2003.
The source to the document was last modified on Wed Mar 12 09:15:16 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Outlines/outline.31.html
.
You may wish to validate this document's HTML ; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu