This exercise consists of a few short functions to help you familiarize yourself with OCaml. You can review the content of the discussion in notes.rb, which includes the topics and examples from the discussion video. Also, check out utop-tutorial.md for an introduction to using utop
.
To test locally, run dune runtest -f
. There is no submission for this assignment, it is just extra practice.
At the top of disc3.ml, there are a few function definitions. Try determining the types of these functions and check your answers with utop
. This portion of the discussion is not tested (and thus not graded), but these kinds of exercises may appear on quizzes and exams!
You will have to fill in definitions for the functions tf1
, tf2
, tf3
such that they have the type that is expected in the .mli
. The operation of the function does not matter, as long as they have the correct types.
- Type:
string -> int
- Type:
'a -> 'b -> 'b -> bool
- Type:
'a list -> 'a list -> 'a
- Note: For this one, you can assume that the lists
a
andb
are not empty.
- Type:
string -> string -> string
- Description: Appends
str2
to the end ofstr1
. - Examples:
concat "" "" = "" concat "" "abc" = "abc" concat "xyz" "" = "xyz" concat "abc" "xyz" = "abcxyz"
- Type:
int -> float -> float
- Description: Adds
integer
andflt
and returns a float representation of the sum. - Examples:
add_to_float 3 4.8 = 7.8 add_to_float 0 0.0 = 0.0
- Type:
int -> int
- Description: Calculates the nth Fibonacci number.
- Examples:
fib 0 = 0 fib 1 = 1 fib 2 = 1 fib 3 = 2 fib 6 = 8
- Type:
int list -> int list
- Description: Adds 3 to each element in
lst
. - Examples:
add_three [] = [] add_three [1] = [4] add_three [1; 3; 5] = [4; 6; 8]
- Type:
int -> int list -> int list
- Description: Given an integer
n
and a listlst
, Remove elements fromlst
that are less thann
. - Examples:
filter 3 [1; 2; 3; 3; 2; 1] = [1; 2; 2; 1] filter 5 [-1; 2; 3; 4] = [-1; 2; 3; 4]
- Type:
'a list -> 'a list
- Description: Given a list
lst
, return a new list that has two copies of every element inlst
. - Examples:
double [1;2;3;4] = [1;1;2;2;3;3;4;4] double ["a"; "b"; "c"] = ["a"; "a"; "b"; "b"; "c"; "c"]