Tail recursion

Exercise 1

Develop a tail-recursive version of the factorial procedure presented in the reading on recursion with integers.

Exercise 2

Develop a tail-recursive version of the iota procedure that takes a natural number n as argument and returns a list of all the natural numbers that are strictly less than n, in ascending order.

Exercise 3

Write a tail-recursive version of the built-in Scheme procedure, reverse which reverses the contents of a list. Why is the tail-recursive version significantly more efficient than the version that I wrote for the model homework?

Exercise 4

Write a deep-recursive version of reverse called deep-reverse, which reverses the contents of a nested list. For example, (deep-reverse '((a b) (c (d e)))) should return (((e d) c) (b a)). Hint: it should look very similar to your reverse procedure from the previous exercise.

Exercise 5

Rewrite the longest-string-in-list procedure, from the lab on local bindings, so that it yields the same result but is tail-recursive. Hint: let your kernel take two arguments: list and longest-sofar.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~stone/courses/scheme/labs/tail-recursion.xhtml

created March 2, 1997
last revised February 17, 2003

John David Stone (stone@cs.grinnell.edu) and Ben Gum (gum@cs.grinnell.edu)