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 Recursion with Lists (1). On to Preconditions and Postconditions.
Held: Friday, 26 September 2003
Summary: Today we continue our exploration of recursion over lists.
Related Pages:
Notes:
Overview:
member? and more.skip, add
1 to the number of skips remaining in the list.
(define tally-skips
(lambda (lst)
(cond
((null? lst) 0)
((eq? (car lst) 'skip)
(+ 1 (tally-skips (cdr lst))))
(else (tally-skips (cdr lst))))))
(tally-skips (list 'hop 'skip 'jump 'and 'skip 'again)) => (tally-skips (list 'skip 'jump 'and 'skip 'again)) => (+ 1 (tally-skips (list 'jump 'and 'skip 'again))) => (+ 1 (tally-skips (list 'and 'skip 'again))) => (+ 1 (tally-skips (list 'skip 'again))) => (+ 1 (+ 1 (tally-skips (list 'again)))) => (+ 1 (+ 1 (tally-skips (list)))) => (+ 1 (+ 1 0)) => (+ 1 1) => 2
tally,
that keeps track of each skip seen.
tally
skip, add 1 to tally
and continue with the remaining values.
tally and continue with
the remaining values.
(define tally-skips-helper
(lambda (lst tally)
(cond
((null? lst) tally)
((eq? (car lst) 'skip)
(tally-skips-helper (cdr lst) (+ 1 tally)))
(else (tally-skips-helper (cdr lst) tally)))))
(tally-skips-helper (list 'hop 'skip 'jump 'and 'skip 'again) 0) => (tally-skips-helper (list 'skip 'jump 'and 'skip 'again) 0) => (tally-skips-helper (list 'jump 'and 'skip 'again) 1) => (tally-skips-helper (list 'and 'skip 'again) 1) => (tally-skips-helper (list 'skip 'again) 1) => (tally-skips-helper (list 'again) 2) => (tally-skips-helper (list) 2) => 2
tally to start as 0, so we write the
primary procedure as
(define tally-skips
(lambda (lst)
(tally-skips-helper lst 0)))
display in the body of the procedure. For example,
(define tally-skips-helper
(lambda (lst tally)
(display (list 'tally-skips-helper lst tally))
(newline)
(cond
((null? lst) tally)
((eq? (car lst) 'skip)
(tally-skips-helper (cdr lst) (+ 1 tally)))
(else (tally-skips-helper (cdr lst) tally)))))
tally-skips.
if. For example, here's
one implementation of member that works on the principle that
.valis a member oflstif the list is not empty andvalis the first element oflstorvalis a member of the remainder oflst
(define member?
(lambda (val lst)
(and (not (null? lst))
(or (equal? val (car lst))
(member? val (cdr lst))))))
Back to Recursion with Lists (1). On to Preconditions and Postconditions.
[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:40 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.18.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby