Computer Science Fundamentals (CS153 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]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Back to More Algorithm Analysis. On to Searching.
Held: Monday, 24 February 2003
Summary: Today we revisit recursion and consider a more efficient way to write recursive procedures. This new technique is called tail recursion.
Related Pages:
Notes:
Overview:
add-to-all, factorial,
traditional right-associative sum.
member?, the left-associative sum.
sum (in class).
(define sumr
(lambda (values)
(if (null? values) 0
(+ (car values) (sumr (cdr values))))))
+ Version 2, left associative, tail recursive.
(define suml
(lambda (values)
(suml-helper values 0)))
(define suml-helper
(lambda (remaining-values partial-sum)
(if (null? remaining-values) partial-sum
(suml-helper (cdr remaining-values)
(+ partial-sum
(car remaining-values))))))
(1 2 3 4) and see if one seems
to be easier to deal with.
(sumr (1 2 3 4)) --> (+ 1 (sumr (2 3 4))) --> (+ 1 (+ 2 (sumr (3 4)))) --> (+ 1 (+ 2 (+ 3 (sumr (4))))) --> (+ 1 (+ 2 (+ 3 (+ 4 (sumr ()))))) --> (+ 1 (+ 2 (+ 3 (+ 4 0)))) --> (+ 1 (+ 2 (+ 3 4))) --> (+ 1 (+ 2 7)) --> (+ 1 9) --> 10
(suml (1 2 3 4)) --> (suml-helper (1 2 3 4) 0) --> (suml-helper (2 3 4) 1) --> (suml-helper (3 4) 3) --> (suml-helper (4) 6) --> (suml-helper () 10) --> 10
things to do once the recursion reaches the base case.
suml:
accumulatespartial solutions. Hence, we often call it an accumulator.
oldbase case (the one in the non-tail-recursive procedure).
Thursday, 15 January 2003 [Samuel A. Rebelsky]
Monday, 24 February 2003 [Samuel A. Rebelsky]
Back to More Algorithm Analysis. On to Searching.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[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:20:37 2003.
The source to the document was last modified on Mon Feb 24 09:14:34 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.21.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby