CS 331 Spring 2026  >  Assignment 2 (Coding in Lua)


CS 331 Spring 2026
Assignment 2 (Coding in Lua)

Assignment 2 is due at 5 pm on Tuesday, February 10. It is worth 75 points.

Procedures

This assignment is to be done individually.

Turn in answers to the exercises below on the UA Canvas site, under Assignment 2 for this class.

Exercises (A–B, 75 pts total)

Exercise A — Running a Haskell Program

Purpose

In this exercise you will make sure you can execute Haskell code.

Instructions

Get the file check_haskell.hs from the class Git repository. This is a Haskell source file for a complete program. When it is executed, this program prints “Secret message #2:”. Below that, it prints a secret message. Run the program. What is the secret message?

There are several ways to execute a Haskell program. One of them is to start up the GHCi interactive Haskell environment, type “:l check_haskell” at the prompt (that is a lower-case ELL after the colon), and then at the next prompt, type “main”.

Exercise B — Programming in Lua

Purpose

In this exercise you will write a Lua module. The module will contain functions dealing with tables, strings, and numbers. One of the functions will be a coroutine. Another will be an iterator function, usable with Lua’s for-in loop.

Instructions

Write a Lua module pa2, contained in the file pa2.lua (note the capitalization!). Module pa2 must export the following four functions, and nothing else: arrayFilter, mostCommon, revSubstr, collatzSeq.

Be sure to follow the Coding Standards.

Test Program

A test program is available in the Git repository: pa2_test.lua. If you run this program (unmodified!) with your code, then it will test whether your code works properly.

To use the test program, place your source file (pa2.lua) in a directory where it can be imported, and then execute the test program file (pa2_test.lua). Do not execute your file.

You may assume that the test program will pass reasonable values to your code. For example, the first argument to mostCommon will always be a string, and the second argument will be an integer that is at least \(0\) and at most the length of the string. The argument to collatzSeq will always be a positive integer, and similarly for the other functions you are to write.

Do not turn in the test program.