Files

Course links

Exercise 1

The file /home/stone/courses/scheme/examples/numbers contains seven hundred and sixteen natural numbers. What is their sum?

Exercise 2

Using sum-of-file as a pattern, write a Scheme procedure file-size that takes as argument a string that names a file and returns the number of characters in that file (that is, the number of times that read-char can be called to read a character from the file without returning the end-of-file object).

Exercise 3

Find out what happens if sum-of-file or file-size is given a string that does not name any existing file.

Exercise 4

Use the store-divisors procedure to draw up a list of the divisors of 120, storing them in a file named divisors-of-120. Examine the file afterwards and confirm that the answer is correct. (Don't give this procedure an extremely large number as argument -- it's too slow. There are more efficient ways to find divisors!)

Exercise 5

The Scheme standard says that if you try to open an output port to a file that already exists, ``the effect is unspecified,'' i.e., anything might happen. Find out what DrScheme does in this situation. (DrScheme actually gives the programmer the opportunity to modify this behavior -- the Help Desk document ``Opening file ports'' describes the possibilities, if you're curious.)

Exercise 6

Two positive integers are said to be relatively prime if they have no common divisors other than 1 -- in Scheme: (= (gcd first second) 1). With store-divisors as a model, write a Scheme procedure store-relative-primes that takes two arguments, the first a string that names the output file to be created and the second a positive integer, and writes into the specified output file every positive integer that is less than the specified positive integer and relatively prime to it.

Exercise 7

Copy the definition of display-string-in-lower-case (in today's reading) into DrScheme and test it by using it to display a mixed-case string in the current output port.

I am indebted to Professor Ben Gum for his contributions to the development of this lab.