



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 5
A Small OCaml Program- Things to Notice
(* A small OCaml program *) let x = 37;; let y = x + 5;; print_int y;; print_string "\n";;
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 11
CMSC 330 12
let pi = 3.14 in pi *. 3.0 *. 3.0;; pi;;
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 23
CMSC 330 24
val y : int list = [1; 2; 3]
val x : int list = [4; 1; 2; 3]
val z : int list = [5; 1; 2; 3]
This expression has type int list but is here used with type int list list
CMSC 330 25
CMSC 330 26
CMSC 330 27
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 *)
CMSC 330 29
CMSC 330 30
Warning: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: []