CSCI 330: Introduction to OCaml

Goals

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.

  1. Download the handout file from Autolab
  2. Update each of the functions with your own solutions. As a reminder, you are expected to complete this individually.
  3. Test your program using the OCaml environment installed on the Linux Lab machines
  4. 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

sumList [1;2;3;4];;

(* int = 10 *)


sumList [1;-2;3;5];;

(* int = 7 *)


sumList [1;3;5;7;9;11];;

(* 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

digitsOfInt 3124;;

(* int list = [3;1;2;4];; *)


digitsOfInt 76543210;;

(* 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

additivePersistence 9876;;

(* 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

digitalRoot 9876;;

(* 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

'a list -> 'a list

Expected Output

listReverse [1;2;3;4];;

(* int list = [4;3;2;1] *)


listReverse ["a";"b";"c";"d"];;

(* 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

palindrome "malayalam";;

(* bool = true *)


palindrome "hannah";;

(* bool = true *)


palindrome "programming";;

(* bool = false *)

Submission

Submit intro.ml as the “Introduction” assignment through Autolab

Grading

75 points total

Attribution

This assignment was adapted from Sorin Lerner’s CSE 130 course at UCSD