x-xs-Computer:~/courses/421/fall-2006/lectures elsa$ ocaml Objective Caml version 3.08.3 # (* Read-eval-print loop; expressions and declarations *) 2 + 3;; (* expression *) - : int = 5 # let test = 3 < 2;; (* declaration *) val test : bool = false # "Hi there";; (* has type string *) - : string = "Hi there" # print_string "Hello world\n";; (* has type unit *) Hello world - : unit = () # (print_string "Bye\n"; 25);; (* Sequence of exp *) Bye - : int = 25 # let a = 3 let b = a + 2;; (* Sequence of dec *) val a : int = 3 val b : int = 5 # let c = let b = a + a in b * b;; val c : int = 36 # b;; - : int = 5 # let x = 5 + 7;; val x : int = 12 # let y = x * 2;; val y : int = 24 # let z = 1.35 + 0.23;; (* Wrong type of addition *) Characters 8-12: let z = 1.35 + 0.23;; (* Wrong type of addition *) ^^^^ This expression has type float but is here used with type int # let z = 1.35 +. 0.23;; val z : float = 1.58 # let u = 1.0 + 2;; Characters 8-11: let u = 1.0 + 2;; ^^^ This expression has type float but is here used with type int # let w = y + z;; Characters 12-13: let w = y + z;; ^ This expression has type float but is here used with type int # true;; - : bool = true # false;; - : bool = false # if y > x then 25 else 0;; - : int = 25 # 3 > 1 & 4 > 6;; - : bool = false # 3 > 1 or 4 > 6;; - : bool = true # let plus_two n = n + 2;; val plus_two : int -> int = # plus_two 17;; - : int = 19 # let plus_two = fun n -> n + 2;; val plus_two : int -> int = # plus_two 14;; - : int = 16 # (fun x -> x * 3) 5;; (* An application *) - : int = 15 # ((fun y -> y +. 2.0), (fun z -> z * 3));; (* As data *) - : (float -> float) * (int -> int) = (, ) # let x = 12;; val x : int = 12 # let plus_x y = y + x;; val plus_x : int -> int = # plus_x 3;; - : int = 15 # let x = 7;; (* Redecaration, Not an update *) val x : int = 7 # plus_x 3;; - : int = 15 # let add_three x y z = x + y + z;; val add_three : int -> int -> int -> int = # let t = add_three 6 3 2;; val t : int = 11 # let h = add_three 5 4;; val h : int -> int = # h 3;; - : int = 12 # h 7;; - : int = 16 # let thrice f x = f (f (f x));; val thrice : ('a -> 'a) -> 'a -> 'a = # let g = thrice plus_two;; val g : int -> int = # g 4;; - : int = 10 # thrice (fun s -> "Hi! " ^ s) "Good-bye!";; - : string = "Hi! Hi! Hi! Good-bye!" # let rec factorial n = if n = 0 then 1 else n * factorial (n - 1);; val factorial : int -> int = # factorial 5;; - : int = 120 # (* better *) let rec factorial n = match n with 0 -> 1 | _ -> n * factorial (n - 1);; val factorial : int -> int = # factorial 5;; - : int = 120 # let s = (5,"hi",3.2);; val s : int * string * float = (5, "hi", 3.2) # let (a,b,c) = s;; val a : int = 5 val b : string = "hi" val c : float = 3.2 # let x = 2, 9.3;; (* tuples don't require parens*) val x : int * float = (2, 9.3) # let d = ((1,4,62),("bye",15),73.95);; val d : (int * int * int) * (string * int) * float = ((1, 4, 62), ("bye", 15), 73.95) # let (p,(st,_),_) = d;; val p : int * int * int = (1, 4, 62) val st : string = "bye" # let fst_of_3 (x,_,_) = x;; val fst_of_3 : 'a * 'b * 'c -> 'a = # s;; - : int * string * float = (5, "hi", 3.2) # fst_of_3 s;; - : int = 5 # fst_of_3 d;; - : int * int * int = (1, 4, 62) # let add_triple (u,v,w) = u + v + w;; val add_triple : int * int * int -> int = # add_triple (6,3,2);; - : int = 11 # add_triple 5 4;; Characters 0-10: add_triple 5 4;; ^^^^^^^^^^ This function is applied to too many arguments, maybe you forgot a `;' # fun x -> add_triple (5,4,x);; - : int -> int = # let triple_to_pair triple = match triple with (0, x, y) -> (x, y) | (x, 0, y) -> (x, y) | (x, y, 0) -> (x, y) | (x, y, _) -> (x, y);; val triple_to_pair : int * int * int -> int * int = # let fib5 = [8;5;3;2;1;1];; val fib5 : int list = [8; 5; 3; 2; 1; 1] # let fib6 = 13 :: fib5;; val fib6 : int list = [13; 8; 5; 3; 2; 1; 1] # (8::5::3::2::1::1::[]) = fib5;; - : bool = true # fib5 @ fib6;; - : int list = [8; 5; 3; 2; 1; 1; 13; 8; 5; 3; 2; 1; 1] # let bad_list = [1; 3.2; 7];; Characters 19-22: let bad_list = [1; 3.2; 7];; ^^^ This expression has type float but is here used with type int # let rec double_up list = match list with [] -> [] (* pattern before ->, expression after *) | (x :: xs) -> (x :: x :: double_up xs);; val double_up : 'a list -> 'a list = # let fib5_2 = double_up fib5;; val fib5_2 : int list = [8; 8; 5; 5; 3; 3; 2; 2; 1; 1; 1; 1] # let silly = double_up ["hi"; "there"];; val silly : string list = ["hi"; "hi"; "there"; "there"] # let rec poor_rev list = match list with [] -> [] | (x::xs) -> poor_rev xs @ [x];; val poor_rev : 'a list -> 'a list = # poor_rev silly;; - : string list = ["there"; "there"; "hi"; "hi"] # let rec map f list = match list with [] -> [] | (h::t) -> (f h) :: (map f t);; val map : ('a -> 'b) -> 'a list -> 'b list = # map plus_two fib5;; - : int list = [10; 7; 5; 4; 3; 3] # map (fun x -> x - 1) fib6;; - : int list = [12; 7; 4; 2; 1; 0; 0] # let rec fold_left f a list = match list with [] -> a | (x :: xs) -> fold_left f (f a x) xs;; val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = # fold_left (fun () -> print_string) () ["hi"; "there"];; hithere- : unit = () # let rec fold_right f list b = match list with [] -> b | (x :: xs) -> f x (fold_right f xs b);; val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b = # fold_right (fun s -> fun () -> print_string s) ["hi"; "there"] ();; therehi- : unit = () #