Week 13: Exercises

1. Rotation

Write a Haskell program that reads a rectangular matrix of integers from standard input. For example:

3 9 8
2 5 12
1 7 4

The program should print out the matrix rotated 90 degrees to the right:

1 2 3
7 5 9
4 12 8

2. Guessing Game

In the lecture we wrote a program in which the computer thinks of a number from 1 to 1000, and the human player has to guess it.

Now write a similar program in which the human player thinks of a number, and the computer has to guess it.

3. Grep

Write a Haskell program that is like the command-line utility 'grep'. It should take two command-line arguments: a string to search for, plus a filename. It should print out all lines in the file that contain the given string.

4. Find

Write a Haskell program that is like the command-line utility 'find'. It should take one command-line argument: a string. The program should look in the current directory and all subdirectories (recursively) for files whose names contain the given string, and print out the (relative) paths of these files, one per line.

5. Infinite Random String

Write a function babble :: RandomGen g => g -> String that produces an infinite string in which random consonants alternate with random vowels. For example, the string might begin "xotafepi..."