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


CS 331 Spring 2025
Assignment 2 (Coding in Lua)

Assignment 2 is due at 5 pm on Tuesday, February 11. 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 2 for this class.

Exercises (A–B, 70 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: mapArray, mostCommon, prefixSuffix, collatz.

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.

You may assume that the test program will pass reasonable values to your code. For example, the parameter to mostCommon will always be a string, and the parameter to collatz will always be a positive integer. However, as noted above, the table passed to mapArray may contain extra keys.

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

Do not turn in the test program.