All homework and programming exercises must be prepared in accordance with the instructions given in the Syllabus. Each assignment must be submitted to Blackboard by its stated deadline.
Do the following programming exercises. You can put all the functions in the same Elm file.
Write an Elm function sort3
that takes 3 integers and returns a 3-tuple with the values in descending order (i.e., largest first).
Write an Elm function sumSqBig
that takes 3 integers and returns the sum of the squares of the two larger integers. (You may use sort3
.)
Write a Elm function adjpairs
that takes a polymorphic list and returns the list of all pairs of adjacent elements. For example, adjpairs [2,1,11,4]
returns [(2,1), (1,11), (11,4)]
.
Write an Elm function mean
that takes a list of numbers and returns the mean (i.e., average) value for the list.
Write an Elm function merge
that takes two increasing lists of integers and merges them into a single increasing list (without any duplicate values). A list is increasing if every element is less than (<
) its successors. Successor means an element that occurs later in the list, i.e., away from the head.
Hint: For merge xs ys
, you might want to consider a pattern match on (xs,ys)
.
We can introduce a new Elm type for the natural numbers (i.e., nonnegative integers) with the definition
type Nat = Zero | Succ Nat
where the constructor Zero
represents the integer value 0 and constructor Succ
represents the “successor function” from mathematics. Thus (Succ Zero)
denotes 1, (Succ (Succ Zero))
denotes 2, and so forth. (See the natural number example on the lecture notes page that gives Lua, Java, Scala, Ruby, and Elixir versions.)
We can define an Elm function natToInt
that takes a Nat
and returns the equivalent value of type Int
as follows:
natToInt : Nat -> Int
natToInt n =
case n of
Zero -> 0
Succ n -> 1 + (natToInt n)
Write the following Elm functions:
intToNat
that takes a nonnegative Int
and returns the equivalent Nat
, for example, 3
returns Succ (Succ (Succ Zero))
.
addNat
that takes two Nat
values and returns their sum as a Nat
. This function cannot use the builtin integer or floating point addition.
Extend the above example by doing ONE of the following:
compNat
that takes two Nat
values and returns the value -1
if the first is less than the second, 0
if they are equal, and 1
if the first is greater than the second. This function cannot use the integer comparison operators.
mulNat
that takes two Nat
values and returns their product as a Nat
. This function cannot use the builtin integer or floating point operations. (Hint: You may want to use addNat
.)
You may want to examine the Elm version of the Rational
case study. In particular, you may want to use the display
function I have there instead of doing your own HTML output. Of course, you are free to be creative with your browser-based interface.
You probably should install Elm on your own computer and use the elm reactor
tool to run the Elm functions. You may also need to install node.js
for some of the tools to run properly.
Elm is NOT available on the Department systems. The Try Elm online editor on the Elm website can be used to solve this problem. However, you will need to save your file by copying and pasting it into a file. There is no way to save/restore the code in the online editor as far as I can tell.
Test your code!
Submit your Elm file(s) to Blackboard. Be sure to document anything the TA or instructor needs to know to grade your work
It is important to work on this assignment before Examination 3, which covers the Elm programming concepts.