Fundamentals of CS I (CS151 2001S) : Outlines
[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
Primary
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
Sets
[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
Links
Reading: Tail Recursion. Lab: Tail Recursion. Back to Tail Recursion. On to Variable-Arity Procedures.
Held Wednesday, April 11, 2001
Summary
Today we continue our consideration of tail recursion, including the construction of tail-recursive procedures that build lists.
Notes
(process-grades "/home/rebelsky/Web/CS151/Examples/samplegrades"
"maxgradereport"
(lambda (grades) (apply max grades)))
(process-grades "/home/rebelsky/Web/CS151/Examples/samplegrades"
"151grades"
(lambda (grades)
(divide (- (sum grades) (min grades))
(- (length grades) 1))))
(list->vector (map proc (vector->list vec)))
c-right-section, the typical call will be
somethings like
> ((cexpt 3) 2) 9 > ((cexpt 2) 3) 8 > (define square ((c-right-section cexp) 2)) > (square 3) 9
cmax was incorrect.
Overview
filter procedure defined below is not tail recursive
because it applies cons the result of one of the recursive calls.
(define filter
(lambda (pred? lst)
(cond
((null? lst) null)
((pred? (car lst)) (filter pred? (cdr lst)))
(else (cons (car lst) (filter pred? (cdr lst)))))))
largest procedure defined below is not tail recursive
because it applies max to the result of the recursive call.
(define largest
(lambda (lst)
(if (null? (cdr lst))
(car lst)
(max (car lst) (largest (cdr lst))))))
(define tr-filter
(lambda (pred? lst)
(let kernel ((unfiltered lst)
(rev-filtered null))
(cond
((null? unfiltered) (reverse rev-filtered))
((pred? (car unfiltered))
(kernel (cdr unfiltered) rev-filtered))
(else (kernel (cdr unfiltered)
(cons (car unfiltered) rev-filtered)))))))
(define tr-largest
(lambda (lst)
(let kernel ((remaining (cdr lst))
(max-so-far (car lst)))
(if (null? remaining) max-so-far
(kernel (cdr remaining)
(max (car remaining) max-so-far))))))
Back to Tail Recursion. On to Variable-Arity Procedures.
[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
Primary
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
Sets
[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
Links
Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.
This page was generated by Siteweaver on Wed May 5 12:15:07 2004.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/outline.38.html.
You may validate
this page's HTML.
The source was last modified Tue Jan 23 16:01:58 2001.