You are on page 1of 4

Lab 4

Ocaml: Basics
1. Introduction
Ocaml is a variant of Caml, which is a functional programming language. Ocaml
system consists of many components, including a native-code compiler
(ocamlopt), a bytecode compiler (ocamlc) and an interpreter (ocaml).

2. Objectives
In this lab, we will try some simple examples, and write some simple functions in
Ocaml. After completing this lab, students will be able to write some simple
functions in Ocaml.

3. Basic
– In Eclipse, change the perspective into Ocaml (Window/Open
Perspective/Other/OCaml)
– Make a new project (File/New/Ocaml Project (ocamlbuild)) named lab4
– In the project lab4, make a new module (Right click on lab4/New/Module)
whose name is experiment
– Select the tab Ocaml Toplevel in the lower right window and type following
expressions in the space below, then press Enter.

10;;
1 + 4;;
5.0 +. 3.0 ;;

– This direct execution is called interpreter. Ocaml is a interpreter, not a


compiler like Java or C++.
– Now we move on to more complex expressions, and get used to Eclipse
editor. Then we can test the expression from the editor to Toplevel Ocaml.
– Type these things into Eclipse editor (below experiment.ml tab).
Remember: add new expressions below your old ones. Don't delete
them because will need the entire file later.

(* This is a comment in Ocaml *)


(* Some definitions *)
let x = 6
let y = 3.14
let str = "hello world!"
(* or a function *)
let div2 x = x / 2
(* which can be applied to a number *)
div2 3
(* but not to a float ! *)
div2 3.0

– Put your cursor to line 3 (let x...) and press F6. What do you see in the
Toplevel Window?
– Put your cursor to line 4 (let y...) and press F6. What is the difference
from above expression? Can you conclude about type inference in Ocaml?
– See line 5. What do you expect in the output? Press F6 to check.
– Do the rest yourself. Compare this syntax with normal function declaration
you learned from high school f(x), do you see the similarity?
– What is the output of line 7? Can you guess what that means? What makes
div2 not accept float? Hint: Look up how "/ operator" works.

4. More advanced

Note: Answer the questions marked as Q into a sheet of paper, written by hand.
You must submit the answers to your teaching assistants before the class.

– Functions with 2 parameters

let avg x y = (x +. y) /. 2.0


(* apply avg with 2 arguments: 3.0 4.0 *)
avg 3.0 4.0

– Q: What is the result of above expression?


– Curry function

let avg_with_3_0 = avg 3.0


(* and try *)
avg_with_3_0 4.0

– Q: What is the type of avg_with_3_0. Can you guess what does curry
function mean?
– Q: Can we use div2 to define avg? Why?
– Q: Define a function float_div2 in order to define avg
– Recursive function with keyword rec (If you want to run multiple lines in
editor, choose the lines and press F6.
let rec fac n = if n=0 then 1 else n * fac (n-1)
(* and try *)
fac 5
(* or in a mathematical way: *)
let rec fac1 =
function
| 0 -> 1
| n -> n * fac1 (n-1)

– Q: Define a function which compute the fibonacci sequence. Reminder: f_0


= f_1 = 1, and for any n > 1: f_n = f_{n-1} + f_{n-2}

You will write the answers for these questions by hand on a paper and submit to
the teaching assistants before the class. The questions are given in the comment.

5. Preparing for the exercises at class

There are two questions in this section. You have to write it into the submission,
too.

(* List is a fundamental datatype in ocaml : *)


let num_list = [1 ; 5; 3; 11; 9]

(* All elements of a list must be in the same type *)


let wrong_list = [1.2; 3.0; 4.2; 5 ]
(* try each line of the following: *)
List.length num_list
List.nth num_list 3
List.map
List.map (fun x -> x * 2) num_list

(* concatenation *)
let l' = [1;2;3] @ [4;5;6]

(* push an element at the beginning *)


let l' = 1 :: [2;3;4;5;6];

(* try redefine the length of a list : *)


let rec my_length l =
match l with
| [] -> 0
| x::l' -> 1 + (my_length l')

(* try to apply my_length *)


my_length [1;2;3;4]

(* Q: write a function to insert an int into the last position of a


list *)

(* and try *)
insert [1;7;5] 9

(* functions are values as any other value, they can be used as


parameters for instance :*)
let rec exist f l =
match l with
[] -> false
| x::l' -> if (f x) then true else exist f l'

(* and try *)
exist (fun x -> x=2) [1;2;3]
(* Q: by using the previous function, write a boolean function which
tell whether there is a negative integer in a list or not. *)

6. What you will do on the class

1. Ask as many questions as you can to the teaching assistants if you have
problems or difficulties with your homework.
2. The TA will ask you to write some more functions based on what you did,
and credit you on the class.

Please copy your entire works (experiment.ml content) to your USB flash drive
or upload somewhere on the internet so that you can do use it in the LAB and do
well with your exercises.

--THE END--

You might also like