Professional Documents
Culture Documents
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 ;;
– 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.
– 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)
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.
There are two questions in this section. You have to write it into the submission,
too.
(* concatenation *)
let l' = [1;2;3] @ [4;5;6]
(* and try *)
insert [1;7;5] 9
(* 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. *)
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--