Held Wednesday, April 11, 2001
Today we continue our consideration of tail recursion, including the construction of tail-recursive procedures that build lists.
(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
filterprocedure 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)))))))
largestprocedure 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))))))
Friday, 12 January 2001
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
You may validate this page's HTML.
The source was last modified Tue Jan 23 16:01:58 2001.