CSCI 330: Introduction to OCaml
Goals
- to gain hands-on experience with OCaml
- to understand the basic fundamentals of functional programming
Overview
You have been tasked with implementing several functions in OCaml. All of the programs require relatively little coding ranging from 2 to 15 lines. If any function requires more than that, you will need to rethink your solution.
- Download the handout file from Autolab
- Update each of the functions with your own solutions. As a reminder, you are expected to complete this individually.
- Test your program using the OCaml environment installed on the Linux Lab machines
- Submit your updated handout file to the “Intro” Autolab assignment.
Start this assignment early I cannot emphasize this enough! ML programming is relatively easy (syntax-wise) but it will seem foreign, especially with recursion and list manipulation.
Setting up your OCaml Environment
We will be using the MUCS Linux Lab to complete laboratory assignments throughout the semester. We have already configured an OCaml environment for us to use.
Visual Studio Code is installed on all of the Linux Lab machines and you can remotely connect to the MUCS servers via Visual Studio Code as well. To setup VS Code to support OCaml, you can click on Extensions (the puzzle piece on the left side) and do a search for “OCaml Platform”.
If you want to load OCaml from the command line, there is a program
called utop
. Just invoking utop should display a REPL
interface. To load a source file into utop
, issue the
following command:
#use "intro.ml";;
Getting Started with OCaml
You are required to implement each of the following functions:
sumList
(10 points)
Accepts a list of integers and returns the sum of the elements of the list.
Signature:
int list -> int
Expected Output
1;2;3;4];;
sumList [
(* int = 10 *)
1;-2;3;5];;
sumList [
(* int = 7 *)
1;3;5;7;9;11];;
sumList [
(* int = 36 *)
digitsOfInt
(10 points)
Accepts a positive integer (strictly > 0) and returns a list representation of all digits of the integer value
Signature:
int -> int list
Expected Output
3124;;
digitsOfInt
(* int list = [3;1;2;4];; *)
76543210;;
digitsOfInt
(* int list = [7;6;5;4;3;2;1;0];; *)
additivePersistence
(10 points)
Consider the process of taking a number, adding its digits, then adding the digits of the number derived from it, etc., until the remaining number has only one digit. The number of additions required to obtain a single digit from a number n is called the additive persistence of n.
Signature:
int -> int
Expected Output
9876;;
additivePersistence
(* int = 2 *)
digitalRoot
(10 points)
Consider the process of taking a number, adding its digits, then adding the digits of the number derived from it, etc., until the remaining number has only one digit. The digit obtained is called the digital root of n.
Signature:
int -> int
Expected Output
9876;;
digitalRoot
(* int = 3 *)
listReverse
(15 points)
NOTE: You may not use any built-in OCaml functions
Write a function which reverses the elements of a list by returning a new list which has the elements of the passed list in reverse order.
Signature
list -> 'a list 'a
Expected Output
1;2;3;4];;
listReverse [
(* int list = [4;3;2;1] *)
"a";"b";"c";"d"];;
listReverse [
(* int list = ["d";"c";"b";"a] *)
palindrome
(5 points)
A palindrome is a string that reads the same from left-to-right as
right-to-left. You may want to use the provided explode
function in addition to listReverse
.
Signature
string -> bool
Expected Output
"malayalam";;
palindrome
(* bool = true *)
"hannah";;
palindrome
(* bool = true *)
"programming";;
palindrome
(* bool = false *)
Submission
Submit intro.ml
as the “Introduction” assignment through
Autolab
Grading
75 points total
- 60 points on passing the autolab tests
- 10 points on submitting relevant tests you’ve used to verify correctness with your implementation
- 5 points on documentation, formatting, describing what your code does, style, and adhering to the specifications and instructions listed
Attribution
This assignment was adapted from Sorin Lerner’s CSE 130 course at UCSD