let test = true ;; (* Remember scope, shadowing *) let x = 1 in (x + x) * (let x = 2 in x + x);; (* A. 4 *) (* B. 8 *) (* C. 16 *) let x = 1 in (x * 2) + (let x = 2 in x + 5) + 4 * x;; (* A. 12 *) (* B. 13 *) (* C. 17 *) (* D. 19 *) let x = (let y = 3 in y * 2) + 5 in let y = 1 in x + y;; (* A. 8 *) (* B. 12 *) (* C. 14 *) (* Side effects (* Still a comment*) NOTE (after lecture): this is still a comment; they can have arbitrary line breaks *) 42 / 0;; Printf.printf "The answer is %d\n" 42;; (* Won't typecheck *) (* let x : int = "Hello";; *) let square x = x * x;; let square (x : int) : int = x * x;; let x = square 3;; let y = square 4;; let z = square 5;; let x = 1 in (let x = 2 in x) + x;; let x = ();; let square = fun x -> x * x in (square 3) + (square 4);; (* + operator as a function *) (+) 1 2;; (-) 4 3;; ( * ) 1 2;; (* Partial application *) let add1 = (+) 1;; add1 41;; (* add1 41 2 won't typecheck -- too many arguments *) add1 99;; let my_add x y = x + y;; (* is syntactic sugar for *) let my_add = fun x -> fun y -> x + y;; let rec fact (n: int) : int = if n <= 0 then 1 else n * fact (n - 1);; let one = fact 0 in one + one;; let rec sum (min: int) (max: int) : int = if max < min then raise (Invalid_argument "sum") else if max = min then min else max + sum min (max - 1) ;; sum 1 5;; sum 5 1;;