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 higher-order procedures by considering procedures that return procedures and by working on some hands-on 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? (opposite-of odd?))
opposite-of
is a procedure that builds
procedures.
(define opposite-of (lambda (pred?) (lambda (val) (not (pred? val)))))
lambda
in the definition of even?
.
opposite-of
.
lambda
expressions in the definition
of opposite-of
?
opposite-of
. The other is the
parameter of the procedure returned by opposite-of
.
odd?
is that it crashes and burns when
given a non-number. Hence, we might like to define an
odd-number?
predicate that holds if its parameter
is both odd and a number.
(define odd-number? (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 make-counter (lambda (pred?) (letrec ((counter (lambda (lst) (cond ((null? lst) 0) ((pred? (car lst)) (+ 1 (counter (cdr lst)))) (else (counter (cdr lst))))))) counter)))
(define count-odds (make-counter odd?)) (define count-evens (make-counter even?))
count-evens
that way
(define count-evens (lambda (lst) (count-evens-helper lst 0))) (define count-evens-helper (lambda (lst count) (cond ((null? lst) count) ((even? (car lst)) (count-evens-helper (cdr lst) (+ 1 count))) (else count-evens-helper (cdr lst) count))))
make-counter
to use that strategy.
(define make-counter (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