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 Recursion with Lists (1). On to Recursion with Natural Numbers (1).
Held: Tuesday, 18 February 2003
Summary: Today we continue our exploration of recursion over lists.
Related Pages:
Assignments
Notes:
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
.
Thursday, 16 January 2003 [Samuel A. Rebelsky]
Monday, 17 February 2003 [Samuel A. Rebelsky]
Back to Recursion with Lists (1). On to Recursion with Natural Numbers (1).
[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:29:48 2003.
The source to the document was last modified on Mon Feb 17 21:31:35 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Outlines/outline.18.html
.
You may wish to
validate this document's HTML
;
;
Check with Bobby