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.

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.

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.

Follow the Coding Standards. Also, I am not providing any test suite for this program; however, you will need to test it.

See “Sample Run”, below, for an example of what running this program might look like.

Other Requirements

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!