CS 331 Spring 2009
Assignment 3
Assignment 3 is due at 5 p.m. Thursday, March 5.
It is worth 25 points.
Procedures
E-mail
answers to the exercises below to
ffggc@uaf.edu,
using the subject
“PA3”.
- Your answers should consist of the file PA3.hs containing the
answers to Exercise A.
This file (or an archive file containing it)
should be attached to your e-mail message.
- Be sure to include your name in your e-mail.
- I may not read your homework e-mail immediately.
If you wish to discuss the assignment (or anything else)
with me, send me a separate message with a different subject line.
Exercises (25 pts total)
Exercise A — Haskell Lists
Purpose
In this exercise, you will write some simple Haskell functions
involving lists.
Instructions
Write a Haskell module PA3,
contained in the file PA3.hs.
Module PA3 should include the following public functions/variables.
- Function evens.
This takes a list and returns a list consisting of all the even-index items in the given list,
where the first item has index 0.
Examples:
- “evens [4,7,5,3,2,8,9,6,1]” should return
[4,5,2,9,1].
- “evens [4,7]” should return
[4].
- “evens [4]” should return
[4].
- “evens []” should return
[].
- “evens "abcdefg"” should return
"aceg".
- Function max2.
This takes a list whose items can be compared with “<”
(this means the item type lies in class Ord, by the way).
It returns a list containing of the largest two items in the given list,
in ascending order.
Exceptions: If the list has length zero or one, then the given list
should be returned.
Examples:
- “max2 [1,9,3,7,4,6]” should return [7,9].
- “max2 [4,5,8,6,8]” should return [8,8].
- “max2 [1]” should return [1].
- “max2 []” should return [].
- “max2 "goblet"” should return "ot".
Function max2 could be written like this:
max2 xs = drop (length xs - 2) $ sort xs -- Must import List, for function sort
However, you are to use a “fold” function (foldl, foldr, foldl1, etc.) to do the bulk of the work.
- Function almostMax.
This takes a list whose items can be compared with “<”.
It returns a list containing the next-to-largest item in the given list,
or the empty list if the given list has length less than 2.
Examples:
- “almostMax [1,9,3,7,4,6]” should return [7].
- “almostMax [4,5,8,6,8]” should return [8].
- “almostMax [1]” should return [].
- “almostMax []” should return [].
- “almostMax "goblet"” should return "o".
Hint: Use your function max2.
- Variable primes.
This is a list of all prime numbers, in ascending order.
Example:
- “take 10 primes” should return [2,3,5,7,11,13,17,19,23,29].
Something you may find useful: the standard prelude contains the function mod,
which is much like the “%” operator in C/C++/Java.
Thus, Haskell “mod 39 12” is like C “39 % 12”; both return 3.
General Requirements
- Your file should begin with comments indicating filename, author, date, and purpose of the file.
- Code should be neat and readable.
- You may use any function or variable in the Haskell standard library.
If you use something that is not in the prelude, you may import whatever portion of the library you require.
- Do not define any public functions or variables other than those listed above.
To make something private, either restrict what the module exports,
or else define it locally (e.g., using “where”).
Test Program
I have written a test program:
pa3_test.hs.
If you compile and run your package with this program (unmodified!), then it will test
whether your package works properly.
If you are using Hugs, then put your file and the test program in the same directory, and do
> :l pa3_test.cpp
> main
(Note that “> ” represents the Hugs prompt,
and is not to be typed.)
Do not turn in pa3_test.hs.