# Class 5: List Internals, Tail Recursion and letrec

Scheme Lab XX.

Homework
• Suppose I define (cos-min i j) to be the minimum of (cos i) (cos i+1) ... (cos j). I might define a scheme function for computing it like this:
```(define (min-cos i j) (if (= i j) (cos i) (min (cos i) (min-cos (+ i 1) j))))
```
Now this isn't tail-recursive, so I can't compute something like (min-cos 1 10000000). Here is a tail-recursive version, along with a redefinition of (min-cos i j) that simply calls the tail-recursive version:
```(define (min-cos-tr i j m)
(if (> i j)
m
(min-cos-tr (+ i 1) j (min (cos i) m))))

(define (min-cos i j) (min-cos-tr (+ i 1) j (cos i)))
```
Study this. Understand this. Love this. Your job is to redefine this as a single function (min-cos i j) that uses a `letrec` to define the tail-recursive version internally. Read the "More functions creating functions: letrec" section of Scheme Lab XX to see how this is done.
• Define a tail-recursive version of the ``` (winner beats? L)``` function from Scheme Lab 4.
If you can't get a working version of `(winner beats? L)`, do a tail-recursive implementation of "sum-of-absolute-valuse":
```(define (sabs L) (if (null? L) 0 (+ (abs (car L)) (sabs (cdr L)))))
```
... that returns the sum of the absolute values of the elements of a list of numbers.
NOTE: Whichever you do, provide an example of calling the function in your file. The user should be able to call this function as before, without having to explicitly pass an extra argument to support the tail recursion (just like you did for the min-cos-tr function above).

Lab xx: List Internals, Tail Recursion and letrec
The material we covered today is all in Scheme Lab XX. The problems posed there do not need to be turned in, just the HW above.

Christopher W Brown