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.
- Your answers must consist of
the answer to Exercise A
(submitted as an attached text/PDF file),
and
the source code for Exercise B
(file
pa2.lua).
- I may not look at your homework submission immediately.
If you have questions,
e-mail me.
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.
- Function
arrayFilter.
This takes a function p and an array t.
Function p can be assumed to be a one-parameter
function that will accept any value in array t.
arrayFilter returns an array containing
every value v in t for which
p(v) is truthy,
in the same order as they appear in t.
No other key-value pairs appear.
Example:
function isBig(n)
return n > 6
end
arr = { 1, 7, 9, 3, 10, 6, 2, 7, 0 }
arr2 = pa2.arrayFilter(isBig, arr)
-- arr2 is the array { 7, 9, 10, 7 }.
- Function
mostCommon.
This function takes a string and a nonnegative integer.
It returns the substring whose length is the given integer
that occurs most often in the given string.
If there is more than one most common substring,
then the one whose first appearance is earliest in the given string
is returned.
If the given string is empty or the number is zero,
then the empty string is returned.
So, for example,
the function call
mostCommon("severance", 1)
will return "e",
since the string "e"
occurs three times in "severance",
while all other strings of length 1 occur at most once.
And
mostCommon("abbbaab", 2)
will return
"ab".
This occurs 2 times.
Each of
"aa"
and
"ab"
occurs just once,
while
"bb"
occurs 2 times,
but its first appearance
comes after the first appearance of
"ab".
The following table gives other examples.
First Argument |
Second Argument |
Return Value |
"severance" |
9 |
"severance" |
"Mississippi" |
1 |
"i" |
"Mississippi" |
2 |
"is" |
"Mississippi" |
3 |
"iss" |
"%%%###" |
1 |
"%" |
"%%%###" |
0 |
"" |
"%%%####" |
1 |
"#" |
"" |
0 |
"" |
"I like coconuts and pears!" |
1 |
" " |
- Coroutine
revSubstr.
This takes a single parameter s, which must be a string.
It yields all substrings of the reverse of s:
first the unique length-zero substring,
then all length-one substrings,
and so on,
ending with the reverse of s itself.
For example, if revSubstr("abca")
is called (as a coroutine),
then it yields the following, in order:
""
"a"
"c"
"b"
"a"
"ac"
"cb"
"ba"
"acb"
"cba"
"acba"
- Function
collatzSeq.
This takes a single parameter k,
which must be a positive integer.
It returns an iterator usable in a for-in loop.
Its iterator goes through one or more integers;
these are the entries in the Collatz sequence
starting at k, as explained below.
Function collatzSeq is usable as follows.
for i in collatzSeq(n) do
ff(i) -- Do something with i
end
The Collatz function [L. Collatz 1937]
is the following function \(c\),
defined on the positive integers.
\[
c(n) = \begin{cases} 3n+1, & \text{if } n \text{ is odd;}\\
n/2, & \text{if } n \text{ is even.}
\end{cases}
\]
The Collatz sequence starting at a positive integer \(k\)
is
\[
k,\ c(k),\ c(c(k)),\ c(c(c(k))),\ \dots,\ 1.
\]
That is, the sequence begins with \(k\),
each later entry is the result of applying function \(c\) to the entry
preceding it,
and the sequence stops when it reaches \(1\).
Let us construct the Collatz sequence starting at \(3\).
- \(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; the sequence stops here.
So the Collatz sequence starting at \(3\) is
\[
3, 10, 5, 16, 8, 4, 2, 1.
\]
Therefore, this code
for i in collatzSeq(3) do
io.write(i.." ")
end
io.write("\n")
will produce the following output:
3 10 5 16 8 4 2 1
If collatzSeq(3) is replaced by collatzSeq(2),
then the output will be the following:
2 1
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.