For this assignment, you will implement several functions using the Reason programming language. You will be provided with the interface for various functions or a specific problem, and you are responsible for implementing the specified functionality.
Project 1 will give you time to practice the concepts we cover in class with respect to the functional programming paradigm. These skills will help prapare you for the remaining programming assignments.
For this assignment, you will be programming in Reason, a dialect of OCaml. There are two parts to this assignment.
You may not work in teams for this project.
Working alone, create and submit the following four (4) artifacts:
p1a.re
, that contains your
implementations of the function interfaces required for P1a.
p1b.re
, that contains your
implementations of the function interfaces required for P1b.
references.txt
providing a citation for each resource you used (excluding class
notes, and assigned readings) to complete the assignment. For
example, if you found a Stack Overflow answer helpful, provide a
link to it. Additionally, provide a brief description of how the
resource helped you. You should create a separate version of
this file for both P1a and P1b.
Unless otherwise noted, you may use standard library functions.
Create a Reason file named p1a.re
, and implement the
following function interfaces.
c_to_f
let c_to_f = (temp: float) : float => {
/* ... */
};
Given a temperature in Celsius, return the temperature in Fahrenheit.
split_tip
let split_tip = ((price: float), (n: int)) : option(float) => {
/* ... */
};
Given the total price
of a meal, and the number of
ways to split the check n
, return
Some(<float>)
, where float is the price with
20% tip split n ways. If n
is less than 1 or
price
is less than 0, return None
.
triangle_area
let triangle_area = ((s1: float), (s2: float), (s3: float)) : option(float) => {
/* ... */
};
Return Some(<float>)
where float is the area of a triangle
with those sides. If the sides do not form a valid triangle, return
None
.
repeat
let repeat = ((f: 'a => 'a), (arg: 'a), (n: int)) : 'a => {
/* ... */
};
Given a unary function f
, an argument to the function
arg
, and an integer n
, return
f
applied to arg
n
times.
list_length
let list_length = (l: list('a)) : int => {
/* ... */
};
Given a list, return the length of the list.
List.length
function.
Create a Reason file named p1b.re
, and implement the
following function interfaces:
salutations
let salutations = (l: list(string)) : list(string) => {
/* ... */
};
Given a list of strings, return a list of strings with greetings.
For example, if you are provided with the following input list
dot_product
let dot_product = ((l1: list(int)), (l2: list(int))) : option(int) => {
/* ... */
};
Given two lists of integers of equal length, return Some
<int>
, where int is the dot product of the two lists.
If the lists are not the same length, return None
.
count
let count = ((l: list ('a)), (e: 'a)) : int => {
/* ... */
};
Given a list of elements, l
, and an element,
e
, return the number of occurrences of e
in l
.
pre_order
let pre_order = (t: int_tree) : list(int) => {
/* ... */
};
Given an integer tree, t
, return a list of the tree
items in the order that they are visited by a pre-order traversal.
int_tree_map
let int_tree_map = ((f: int => int), (t: int_tree)) : int_tree => {
/* ... */
};
Given a unary function, f
, and an integer tree,
t
, return a new integer tree with f
applied to all nodes of t
.
For this part of the assignment, one resource is provided:
Documentation is your friend! Here are some external resources you might find helpful:
Recall that Reason is a dialect of OCaml. You are able to make use all of OCaml's standard library and pervasives as a result. While it is possible to implement all of the function interfaces on your own, you may find it easier to use library functions in some cases (check out the List module in particular).
When implementing code in Reason, make a particular effort to use local bindings to:
You will most likely need to define recursive helper functions. To improve the performance of your code, be sure to make your functions tail-recursive whenever possible.
If you are still stuck, you can post on Piazza or approach the professor.
Video guides are provided to help you get started with various aspects of this project. Note that there may be errors and mistakes in these videos. You should not blindly follow the steps taken in these videos!
You must turn in a tar.gz file containing these files:
references.txt
: your file of citationsp1a.re
(contains your answers for P1a) You must turn in a tar.gz file containing these files:
references.txt
: your file of citationsp1b.re
(contains your answers for P1b)
Note, you can use the CS-364 Makefile
to generate a
submission archive:
make fullsubmit
The Makefile
is available here.
Be sure to update the IDENTIFIER
and EXECUTABLE
variables appropriately.
P1 Grading (out of 50 points):