Lab: Haskell Mains
In today's lab, you'll develop several standalone Haskell programs that emulate the command-line utilities head
, tail
, and seq
. In particular, you will gain experience with do
blocks, the IO
system, command-line arguments, Haskell lists, and iteration via recursion.
Head
Write a main
in head.hs
that emulates the command-line head
utility, which prints just the first n
lines of a file. Suppose the file planets.txt
contains the names of the planets, one planet per line. The command runhaskell head.hs planets.txt 6
produces this output:
Mercury
Venus
Earth
Mars
Jupiter
Saturn
You read about the lines
function, which is helpful for this task. So is unlines
. Also check out common list functions.
This program can be written in fewer than 10 lines of code.
Tail
Write a main
in tail.hs
that emulates the command-line tail
utility, which prints just the last n
lines of a file. The command runhaskell tail.hs planets.txt 4
produces this output:
Jupiter
Saturn
Uranus
Neptune
Your solution should be very similar to head.hs
. This program can be written in around 10 lines of code.
Seq
Write a main
in seq.hs
that emulates the command-line seq
utility, which prints a sequence of numbers between the two integers passed as command-line arguments. For example, the command runhaskell seq.hs 6 11
produces this output:
6
7
8
9
10
11
The sequence may also be descending. The command runhaskell seq.hs 2 -1
produces this output:
2
1
0
-1
Ease the implementation by writing a helper function that takes in three parameters: a current value, a target value, and a delta. Have it print the current value and then decide whether to recurse.
This program can be written in fewer than 20 lines of code.
Submit
To receive credit for this lab, you must submit your .hs
scripts on Canvas by Monday noon. Late labs or forgot-to-submits are not accepted because Monday at noon is when your instructor has time to grade.