CSCI 330 — Lab 1

Due: Friday February 9, 2018 @ 5:00PM

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 Our Files Directory
  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 "Lab 1" 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. I have already configured an OCaml environment for us to use. To load the environment, you will want to issue the following command from a terminal:

source /home/faculty/wkillian/.opam/opam-init/init.sh

To make this work when you login the to the linux lab, you can issue the following command (NOTE: you should only run this once)

touch ~/.bash_profile && opam config env --root=/home/faculty/wkillian/.opam >> ~/.bash_profile

An OCaml IDE? It's more likely than you think!

Visual Studio Code is installed on all of the Linux Lab machines. You can click on Extensions (the puzzle piece on the left side) and do a search for "OCaml". I've tested the first one listed (it's called "OCaml" -- though it is marked deprecated, you can still install and use it). Toggling various options can be done with a Ctrl+Shift+P keyboard shortcut and doing a search for "OCaml"

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 "lab1.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 (20 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 (10 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 lab1.ml as the Lab 1 assignment through Autolab

Grading

100 points total

Attribution

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