









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










CMSC 330 2
CMSC 330 3
CMSC 330 4
CMSC 330 7
CMSC 330 8
% ocamlc ocaml1.ml % ./a.out 42 %
(* A small OCaml program *) let x = 37;; let y = x + 5;; print_int y;; print_string "\n";;
ocaml1.ml:
CMSC 330 9
val x : int = 37
val y : int = 5
val z : int = 42
42- : unit = ()
Colorless green ideas sleep furiously- : unit = ()
This expression has type string but is here used with type int
CMSC 330 10
% ocaml Objective Caml version 3.08.
val x : int = 37 val y : int = 42 42- : unit = ()
(* A small OCaml program *) let x = 37;; let y = x + 5;; print_int y;; print_string "\n";;
ocaml1.ml:
CMSC 330 13
let pi = 3.14 in pi *. 3.0 *. 3.0;; pi;;
{ float pi = 3.14;
pi * 3.0 * 3.0; } pi;
CMSC 330 14
let pi = 3.14 in let r = 3.0 in pi *. r . r;; ( pi, r no longer in scope *)
{ float pi = 3.14; float r = 3.0; pi * r * r; } /* pi, r not in scope */
CMSC 330 15
let next x = x + 1;; next 3;; let plus (x, y) = x + y;; plus (3, 4);;
CMSC 330 16
let area r = let pi = 3.14 in pi *. r *. r
let area d = let pi = 3.14 in let r = d /. 2.0 in pi *. r *. r
CMSC 330 19
let print_both (s, t) = print_string s; print_string t; "Printed s and t."
"Printed s and t."
CMSC 330 20
CMSC 330 21
struct list { int elt; struct list *next; }; … struct list *l; … i = 0; while (l != NULL) { i++; l = l->next; }
CMSC 330 22
CMSC 330 25
CMSC 330 26
CMSC 330 27
CMSC 330 28
match e with p1 -> e1 | ... | pn -> en
let is_empty l = match l with [] -> true | (h::t) -> false
is_empty [] (* evaluates to true ) is_empty [1] ( evaluates to false ) is_empty [1;2;3] ( evaluates to false *)