CS 331 Spring 2025 > Assignment 5 (Coding in Haskell)
CS 331 Spring 2025
Assignment 5 (Coding in Haskell)
Assignment 5 is due at 5 pm on Tuesday, March 25 Thursday, March 27. It is worth 70 points.
Procedures
This assignment is to be done individually.
Turn in answers to the exercises below on the UA Canvas site, under Assignment 5 for this class.
- Your answers must consist of
the answer to Exercise A
(submitted as an attached text/PDF file),
the source code for Exercise B
(file
PA5.hs
), and the source code for Exercise C (filesum.hs
). - I may not look at your homework submission immediately. If you have questions, e-mail me.
Exercises (A–C, 70 pts total)
Exercise A — Running a Prolog Program
Purpose
In this exercise you will make sure you can execute Prolog code.
Instructions
Get the file check_prolog.pl
from the class Git repository.
This is a Prolog source file for a complete program.
When it is executed,
this program prints
“Secret message #4:
”.
Below that, it prints a secret message.
Run the program.
What is the secret message?
The Prolog implementation we will be using is
SWI-Prolog.
To execute the file, run SWI-Prolog,
At the “?-
” prompt, type:
[Interactive Prolog]
?- ['check_prolog.pl'].
Above, “?-
” is the prompt;
do not type this.
Be sure you end with a period.
At the next prompt, type:
[Interactive Prolog]
?- main.
Again, do not type the prompt, and be sure to end with a period.
After you type “main.
”,
the program runs.
Exercise B — Haskell Functions & Variables
Purpose
In this exercise, you will write a Haskell module containing functionality dealing with lists and numbers.
Instructions
Write a Haskell module PA5
,
contained in the file PA5.hs
(note the capitalization!).
Module PA5
must contain the following
public functions/variables:
collatzCounts
,
>#
(infix operator),
superfilter
,
listSearch
,
alternatingSums
.
To write your module, start your file (after the initial comments) with
[Haskell]
module PA5 where
After that, define the various functions, operators, and variables as usual. Be sure to follow the Coding Standards.
I have provided an incomplete “skeleton”
version of file
PA5.hs
;
this is in the Git repository.
This file has the above line, along with dummy
versions of the required functions/variables.
You may use this file as the basis for your own work.
Here are the details on the five things that
are to be defined in file PA5.hs
.
- Variable
collatzCounts
. This is a list ofInteger
values. Item \(k\) (counting from zero) ofcollatzCounts
tell how many iterations of the Collatz function are required to take the number \(k+1\) to \(1\).The Collatz function is the following function \(c\).
\[ c(n) = \begin{cases} 3n+1, & \text{if } n \text{ is odd;}\\ n/2, & \text{if } n \text{ is even.} \end{cases} \]
So, for example, item 0 of
collatzCounts
is0
, since no applications of the Collatz function are required to make \(0+1=1\) turn into \(1\). Item 2 ofcollatzCounts
is7
. Since, starting with \(2+1=3\), it requires \(7\) steps to get to \(1\).- \(3\) is odd, so \(c(3) = 3\cdot 3 + 1 = 10\).
- \(10\) is even, so \(c(10) = 10/2 = 5\).
- \(5\) is odd, so \(c(5) = 3\cdot 5 + 1 = 16\).
- \(16\) is even, so \(c(16) = 16/2 = 8\).
- \(8\) is even, so \(c(8) = 8/2 = 4\).
- \(4\) is even, so \(c(4) = 4/2 = 2\).
- \(2\) is even, so \(c(2) = 2/2 = 1\).
- \(1\) has been reached; we stop here.
Example:
take 10 collatzCounts
returns[0,1,7,2,5,8,16,3,19,6]
.
Something you may find useful: The Haskell function
div
does integer division. For example,div 57 10
returns5
. - Infix operator
>#
. The two operands are lists of the same type. The return value is anInt
giving the number of indices at which the item in the first list is greater than the item in the second list.Examples
[1,2,3,4,5] ># [1,1,3,2,9,9,9,9]
returns2
.[] ># [1,1,3,2,9,9,9,9]
returns0
."axcxex" ># "abcde"
returns2
."fffff" ># "abcde"
returns5
.
- Function
superfilter
. This takes a predicate and two lists. It returns a list of all items in the second list for which the corresponding item in the first list makes the predicate returnTrue
.Examples:
superfilter (>0) [-1,1,-2,2] [1,2,3,4,5,6]
returns[2,4]
.superfilter (==1) [2,2,1,1,1,1,1] "abcde"
returns"cde"
.
- Function
listSearch
. This takes a list and a value whose type is the same as the type of the values in the list. It returns aMaybe Int
. It the value is equal to an item in the list, then the return value isJust n
, wheren
is the earliest index (starting from zero) at which the value is found in the list. If the value is not found in the list, then the return value isNothing
.Examples:
listSearch "abcde" 'd'
returnsJust 3
.listSearch "abcde" 'x'
returnsNothing
.listSearch [2,1,2,1,2] 1
returnsJust 1
.listSearch [5,5,5,5,5] 5
returnsJust 0
.
- Function
alternatingSums
. This takes a list of numbers. It returns a tuple of two numbers: the sum of the even-index items in the given list, and the sum of the odd-index items in the given list. Indices are zero-based.You must implement
alternatingSums
using a “fold” function:foldl
,foldr
,foldl1
, orfoldr1
, as follows. (The “…
” below are replaced by other code.)alternatingSums xs = fold… … xs where …
If function
alternatingSums
is not implemented like this, then it will not be graded.Examples:
alternatingSums [1,2,3,4]
returns(4,6)
.alternatingSums [20]
returns(20,0)
.alternatingSums [20.0]
returns(20.0,0.0)
.alternatingSums []
returns(0,0)
.alternatingSums [1,1,1,1,1,1,1]
returns(4,3)
.
Test Program
A test program
is available in the Git repository:
pa5_test.hs
.
If you compile and run this program (unmodified!) with your code,
then it will test
whether your code works properly.
To run the test program,
put both file pa5_test.hs
and your file PA5.hs
in the appropriate directory.
Start up GHCi,
and type the following two lines at the prompt.
[Interactive Haskell]
> :l pa5_test.hs > main
Do not turn in the test program.
Exercise C — A Complete Haskell Program
Purpose
In this exercise, you will write a simple interactive program of the kind written in some Computer Science I assignments. However, the program will be in Haskell.
Instructions
Write a Haskell program
contained in the file sum.hs
.
When executed
(that is, when function main
is called),
it must do the following.
- Print an explanatory message.
- Input a sequence of integers from the user, one on each line, ending with a blank line.
- Print the sum of the numbers entered. Note that the sum of no numbers is zero.
- Ask the user whether to repeat the program. If so, start over; if not, exit.
See “Sample Run”, below, for an example of what running this program might look like.
Other Requirements
- When the program runs, it needs to be clear what the program is for, and what the user is expected to do.
- Robust programs (those than can handle any input whatsoever) are nice, but they can be difficult to write. Your program must either be robust, or it must contain, near the beginning of the source code, a prominent comment indicating what input it can handle. (I am thinking primarily about what happens if the user enters something that is not a number.)
- Your code must run reasonably quickly.
Sample Run
Here is a run of my version of sum.hs
,
colored as usual to distinguish user input and program output.
Enter a sequence of integers, one on each line. I will compute the sum of the integers. Enter number (blank line to finish): 1 Enter number (blank line to finish): 5 Enter number (blank line to finish): 2 Enter number (blank line to finish): Sum: 7 Compute another sum? (y/n) y Enter a sequence of integers, one on each line. I will compute the sum of the integers. Enter number (blank line to finish): Sum: 0 Compute another sum? (y/n) y Enter a sequence of integers, one on each line. I will compute the sum of the integers. Enter number (blank line to finish): 5 Enter number (blank line to finish): 1 Enter number (blank line to finish): 100 Enter number (blank line to finish): -8 Enter number (blank line to finish): 100 Enter number (blank line to finish): 37 Enter number (blank line to finish): Sum: 235 Compute another sum? (y/n) n Bye!