# Class 38: Tail Recursion, Continued

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

• The questions on exam 2 just keep on coming.
• Problem 1: Grading
• Each line of the input file is a list.
• The file itself is not a list.
• You can assume that the first three elements of each list are first-name, last-name, and e-mail.
• You can assume that the remaining values are all numbers.
• The infile and outfile parameters are strings that name files.
• You will call your procedure with something like:
```(process-grades "/home/rebelsky/Web/CS151/Examples/samplegrades"
```
or
```(process-grades "/home/rebelsky/Web/CS151/Examples/samplegrades"
(- (length grades) 1))))
```
• Problem 2: Mapping on Lists
• No, you cannot use
```(list->vector (map proc (vector->list vec)))
```
• Problem 3: Currying
• For `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
```
• Yes, one of the results for `cmax` was incorrect.

Overview

• A few more notes on tail recursion
• Lab

## Some More Notes

• It should be easy to identify non-tail recursive procedures:
• They're recursive.
• In at least one recursive call, you apply some procedure to the result of the recursive call.
• The `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)))))))
```
• The `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))))))
```
• Yes, we can make both of these procedures tail recursive, but with varying degrees of difficulty.
```(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))))))
```

## Lab

• Continue the lab.

## History

Friday, 12 January 2001

• Created generic outline format for class.

Back to Tail Recursion. On to Variable-Arity Procedures.

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`.