CSC 153 Grinnell College Spring, 2005 Computer Science Fundamentals Laboratory Exercise

Iteration

Goals

This laboratory exercise provides practice with iteration through the use of Scheme's do-expression.

Steps for this Laboratory Exercise

1. Write a procedure `display-count` that takes two arguments, `start` and `finish`, and counts upwards from `start` to `finish`, displaying each number on a separate line. (The preconditions are that `start` and `finish` must both be exact integers and `start` must be less than or equal to `finish`.) The `display-count` procedure should return the number of lines of output it produces.

2. Review the `sqrt-table` procedure from the lab on input and output. This procedure takes one argument, a non-negative integer, and prints out a table of integers and their square roots, for integers equal to or less than its argument.

In printing the table of square roots, procedure sqrt-table used a helper function to handle the printing of each line, after the initial header was printed.

Rewrite the sqrt-table-helper function by using a `do`-expression.

3. Define a Scheme procedure that takes any non-empty list of real numbers as its argument and returns the number that is the greatest element of the list. Use a `do`-expression to run through the positions of the list.

4. Define a `sum` procedure, which takes any list of numbers as its argument and returns their sum. In your procedure, use a `do`-expression to process list elements iteratively.

5. Write a procedure that counts the number of vowels that occur in a string. Within your procedure, keep the string in tact -- do not convert it to a list. Also, use a do-expression for any looping. Finally, keep the number of variables in your do-expression to a minimum -- for example, the string variable should not be redefined in the loop.

6. Rewrite the encode-char procedure (including its kernel procedure) from the lab on strings, so that all looping is done using a do-expression.

This document is available on the World Wide Web as

```http://www.cs.grinnell.edu/~walker/courses/153.sp05/labs/lab-iteration.shtml
```