Lab: Higher-Order Mains

Dear Computer

Chapter 8: Functions Revisited

Lab: Higher-Order Mains

In today's lab, you'll develop several standalone Haskell programs that can be expressed readably and swiftly with higher-order functions, lambdas, partial application, and composition. You will continue to gain experience with do blocks and the IO system.

Complements

Write a main in complements.hs that accepts a path to a file full of numbers, one per line. It prints just those numbers whose 100-complements are also in the file. The 100-complement of 62 is 38 since they add to 100. Suppose the file contains these lines:

62
45
90
38
80
10

Then the output is:

62
90
38
10

This program can be written in around six lines of code, not counting import statements. Consider using functions mapM_ and elem, in addition to the higher-order functions we've discussed. Ensure that your script works with any file of numbers, not just the example.

Mountain

Write a main in mountain.hs that reads in the file whose path is passed as a command-line argument and prints out the file in line-length order, with its shortest line at the top and its longest line at the bottom.

For example, consider this file:

An honest man has hardly need to count more than
his ten fingers, or in extreme cases he may add
his ten toes, and lump the rest. Simplicity,
simplicity, simplicity! I say, let your affairs
be as two or three, and not a  - hundred or a
thousand; instead of a million count half a
dozen, and keep your accounts on your thumb nail.

When printed as a mountain, it has this form:

thousand; instead of a million count half a
his ten toes, and lump the rest. Simplicity,
be as two or three, and not a  - hundred or a
his ten fingers, or in extreme cases he may add
simplicity, simplicity! I say, let your affairs
An honest man has hardly need to count more than
dozen, and keep your accounts on your thumb nail.

This program can be written in around five lines of code, not counting import statements. Use the sortWith function from GHC.Exts to sort the lines. Ensure that your script works with any file.

Grep

Write a main in grep.hs that accepts as command-line arguments a search string and one or more file paths. It reads in each of the files and prints just those lines of each file that contain the search string. For example, suppose you have the file one.txt:

green 2
cornflower 1
pink 2

And the file two.txt:

978962
78634
8456
508690

When you run runhaskell grep.hs 2 one.txt two.txt, you see this output:

green 2
pink 2
978962

This program can be written in around seven lines of code, not counting import statements. To ease the implementation, define a helper function grepFile that processes an individual file. Have it accept the search string and a single path as parameters. Use the isInfixOf function from Data.List to see if a string contains the pattern.

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.

← Lecture: Embracing Functions