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**

- 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:
or
(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))))

- Problem 2: Mapping on Lists
- No, you cannot use
(list->vector (map proc (vector->list vec)))

- No, you cannot use
- 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.

- For

- Problem 1: Grading

**Overview**

- A few more notes on tail recursion
- Lab

- 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))))))

- Continue the lab.

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.