0% found this document useful (0 votes)
49 views2 pages

Ccps506 Lab2 Elixir

The document describes an Elixir programming lab assignment involving list operations and tail recursion. It provides 8 functions that students must implement to practice list manipulation and recursive thinking without built-in shortcuts or iteration.

Uploaded by

Joash Macenton
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views2 pages

Ccps506 Lab2 Elixir

The document describes an Elixir programming lab assignment involving list operations and tail recursion. It provides 8 functions that students must implement to practice list manipulation and recursive thinking without built-in shortcuts or iteration.

Uploaded by

Joash Macenton
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CCPS506 Lab 2

Elixir: List operations and tail recursion


Preamble
In this lab you’ll be working with lists and writing tail recursive functions in Elixir. Each of
these questions would be very easy in Python using indexing and loops, but in functional
languages recursion and list operations are the tools we’ve got.

Lab Testing and Code Structure


Provided with this lab description is an Elixir mix project containing several different folders
and files. In the lib directory is a file called lab2.ex which contains the module Lab2. You will
add your functions to this module. In the folder test is a file called lab2_test.exs. This file
contains several test cases for each function. Feel free to add your own tests if you wish.
You can run these test cases on your code using the ‘mix test’ command from the top-level
lab2 directory.
The ‘mix test’ command can be run from your own terminal if you installed Elixir locally. If
you’re working in replit, this project can be uploaded there as well. Simply drag the
contents of the lab2 directory into your replit file system.

Lab Description
Eight functions are described below. Functions 1-4 do not require recursion at all, while
functions 5-8 do. Your solutions for these functions should be tail recursive. No built-in
shortcuts are permitted, no indexing, no attempted iteration. Your functions must call
themselves in some tail recursive fashion. Traditional recursion will not be eligible for full
marks.

1) firstTwoEqual?/1 – This function accepts a list as input. It should return true if the
first two elements of the list are the same, and false otherwise. If the input list contains
fewer than two elements, you should also return false.

2) nextNineNine/1 – This function accepts a list as input. It should insert the integer 99
into the second position of the list and return the resulting list. If the input list is empty,
return the :error atom.

3) frontToBack/1 – This function accepts a list as input. It should remove the head
from the list and append it to the back of the list. Return the resulting list. If the input
list is empty, return the :error atom.
4) canBeTriangle?/1 – This function will receive a tuple as an argument. It should
return true if the elements of this tuple could be the side lengths of a triangle, and false
otherwise. This means that if the function receives as input anything other than a 3-
tuple, return false. If the function receives a 3-tuple, but the elements are not numeric,
return false also.
Beyond the above, for example, the numbers 3, 4, 5 could represent the side lengths of
a triangle, but 1, 2, 5 could not. (To see why this is the case, try and draw a triangle with
side lengths 1, 2, and 5. Use a ruler.)

5) isAscending?/1 – This function should return true if the elements of the input list
are strictly ascending, and false otherwise. Each element must be strictly larger than
(not merely equal to) the element that precedes it. Note that the empty list is
ascending, as is every list containing only a single element.

6) onlyOddDigits?/1 – This function should return true if the provided integer


argument contains only odd digits (1, 3, 5, 7, 9) and false otherwise. Note that the digit
'0' is considered even. Hint: The functions rem/2 and div/2 will serve you well here.

7) tailFib/1 – This function accepts an integer argument, n, and returns the nth
Fibonacci number. Assume the first two Fibonacci numbers are 1 and 1. That is,
tailFib(1) == 1, tailFib(2) == 1. There is no tailFib(0). Your tail-recursive implementation
must avoid the double recursive call. No O(2n)!

8) giveChange/2 – This function accepts an amount of money, and a list of coin values
(in descending order). The function should return a list containing the minimum number
of coins necessary to represent the money amount. The returned list should be in
descending order. If it is not possible to make change for the amount with the provided
list of coins, you should return the :error atom. Several examples are below.
amount: coins: expected result:
64 [50, 25, 10, 5, 1] [50, 10, 1, 1, 1, 1]
100 [42, 17, 11, 6, 1] [42, 42, 11, 1, 1, 1, 1, 1]
64 [50, 25, 10, 5] :error
You may assume the coin values are provided such that the greedy solution will always
be correct. Subtract the largest coin possible at each step.

Submission
Labs are to be completed and submitted individually. Submit your lab2.ex file containing
the Lab2 module and all completed functions on D2L, under the submission for Lab2. You
do not need to submit the tester file.

You might also like