COMP1011 Exercises for Week 07
Computing 1A 05s2 Last updated Fri 02 Sep 2005 17:07
Mail cs1011@cse.unsw.edu.au

Lab Exercises Week 7

Input & Output

Ex 1:

Using ghc

For the first component of Ex. 1, create a file Lab_io_1a.hs. Within this file, implement a Haskell module called Main. This module when compiled with ghc and executed at the shell prompt should print a list containing the numbers from 1 to 10 to the console - i.e.,

enki chak 6 (../html/labs): ghc -o lab_io_1a Lab_io_1a.hs
enki chak 7 (../html/labs): ./lab_io_1a
[1,2,3,4,5,6,7,8,9,10]

Note: In the first line, the text "enki chak 7 (../html/labs):" is the shell prompt - ie, the text printed by the Unix shell to indicate that it is ready to accept the next command - and the text "ghc -o lab_io_1a Lab_io_1a.hs" is the Unix command invoking the Haskell compiler.

Counting vowels

Your next task is to create an interactive program that, upon entry of a line of text, counts the number of vowels in this text. The program code should reside in a file called Lab07-1b.hs and implement a Haskell Main module.

enki chak 12 (.../html/labs): ghc -o lab_io_1b Lab_io_1b.hs
enki chak 13 (.../html/labs): ./lab_io_1b
Enter a line of text:
Your next task is to create an interactive program.
There are 17 vowels in this text.

Here the text "Enter a line of text:" and "There are 17 vowels in this text." is printed by the program, whereas "Your next task is to create an interactive program." is the user input.

When you have completed the above exercises show them to your tutor for this week's core mark.

Ex 2:

Start a new file called Lab_io_2.hs into which you add your solutions to Ex. 2. The file should implement a Haskell Main module.

Splitting a string into lines: lines

Write a recursive function that given a string spanning multiple lines (i.e., containing '\n' characters), returns a list of strings - one per line (with the newline characters removed).

lines :: String -> [String]

Here are some example runs:

Main> lines "\nsome\n\nstrings are strange\n"
["","some","","strings are strange"]
Main> lines "This string\ncontains\nthree lines."
["This string","contains","three lines."]
Main> lines ""
[]
Main> lines "\n"
[]

IMPORTANT NOTE: There is already a function called lines in the Prelude. To avoid that this definition interferes with yours, put

import Prelude hiding (lines)

at the very beginning of your module. You must not use the Prelude function lines in your solution.

Adding line numbers to a file

Now, you should extend the module containing the lines functions with some I/O. When executed after being compiled with ghc, the program should ask for a filename, read the file, add line numbers in front of each line, and finally print all lines to the console.

For example, given that the contents of the file example.txt is

<H4>Adding line numbers to a file</H4>
<p>
  Now, you should extend the module containing the 
  <code>lines</code> functions with some I/O.  
  When executed after being compiled with 
  <code>ghc</code>, the program should ask 
  for a filename, read the file, add line numbers 
  in front of each line, and finally print all 
  lines to the console.

A run of Lab07-2.hs should look as follows:

enki chak 8 (../html/labs): ghc -o lab_io_2 Lab_io_2.hs
enki chak 9 (../html/labs): ./lab_io_2
Enter filename: example.txt
1: <H4>Adding line numbers to a file</H4>
2: <p>
3: Now, you should extend the module containing the 
4: <code>lines</code> functions with some I/O.  
5: When executed after being compiled with 
6: <code>ghc</code>, the program should ask 
7: for a filename, read the file, add line numbers 
8: in front of each line, and finally print all 
9: lines to the console.

Hints: In addition to your implementation of lines, the function unlines that we discussed in the lecture will be helpful (unlines is also defined in the Prelude, so you can just use it without any additional definitions). Furthermore, in addition to implementing the I/O, you will have to write another recursive function to add the line numbers. Moreover, you have to import the module IO and to use the statement hSetBuffering stdout NoBuffering as the first action in main to allow output and input on the same line.

When you have completed the above exercise show them to your tutor for this week's advanced mark.

Additional Exercise

The following is an additional exercise to help you getting practice in programming. It is not part of the lab mark, but it should help you understand some of the material covered in the lecture.

Interactive input

Write a variant of the program from Ex. 2 that instead of reading the input from a file, reads lines of text from the keyboard until an empty line is entered. Then, it adds line numbers to these lines of text and outputs them as before.