(* Simple expressions *) 34 + 8;; 27.0 +. 9.4;; "hello";; (* Hello, World! *) print_string "Hello, world!\n";; (* Basic Types *) 20.3;; let answer = 42;; (answer < 50);; "Ravi";; 1 + "hello";; 2.0 +. 4;; (* Value Creation 1: Global Let *) let a = 42;; a * 6;; let i = 20;; let a = 10;; a * 2;; (* Value Creation 2: Local Variables *) let i = 10;; let a = 20;; let i = a + a in i * a;; i * a;; (* Value Creation 3: Function Definition *) let i = 10;; let f n = i + 10;; f i;; let i = 100;; f i;; (* Alternate Syntax *) let f n = n + 10 ;; f 20;; let f = fun n -> n + 10 ;; f 20;; let g x y = x + y ;; g 5 6 ;; let g = fun x y -> x + y ;; g 5 6 ;; let g = fun x -> fun y -> x + y;; g 5 6 ;; (* Function calls *) let i = 10;; let f n = i + 10;; f i;; let i = 100;; f i;; (* Anonymous Functions *) (fun n -> n + 10) 20;; (fun x y -> x + y) 3 4;; (* Functions as Parameters *) (fun x y -> x y) (fun x -> x + 5) 4;; let thrice f x = f(f(f x));; let inc n = n + 1;; thrice inc 4 ;; let inc3 = thrice inc;; inc3 4;; (* Problem 1 *) let x = 20;; let f = fun x -> x + 1 ;; let y = f 30;; print_int x;; (* Function Types *) let inc x = x + 1;; inc 5;; inc 2.4;; (* Functions with Multiple Parameters *) let triangle a b = a * a + b * b ;; triangle 3 4;; let t3 = triangle 3;; t3 4;; (* Local Let *) let triangle a b = let asq = a * a in let bsq = b * b in asq + bsq;; (let x = 10 in x) + (let x = 20 in x);; (* Scoping Question *) let x = 27;; let foo x = let x = 5 in (fun x -> print_int x) 10;; foo 12;; (* Tuples *) let t = 2,3 ;; let t2 = (3,42) ;; let t3 = (2, "string", true, fun x -> x + x);; let t4 = (1,(2,(3,4))) ;; (* Pulling tuples apart *) let t3 = (2, "string", true, fun x -> x + x);; let (a,b,c,d) = t3;; let (n,_,_,f) = t3 in f n;; (* Pattern Matching *) let iszero n = match n with | 0 -> "it's zero" | 1 -> "it's one" | _ -> "not zero or one";; iszero 1;; (* Alternate Syntax *) let iszero = function 0 -> "it's zero" | 1 -> "it's one" | _ -> "not zero or one";; let getfirst = function (a,b,c) -> a ;; getfirst (1,2,3) ;; getfirst ("Test",5,2.73) ;; (* Variable Creation Method 4: Matching *) let inctup a = match a with | (0,y) -> y,1 | (x,y) -> x+1,y+1 ;; inctup (2,3) ;; inctup (0,3);; (* List Examples *) [] ;; let empty = [] ;; let single = [1] ;; let rlist = [2.3; 4.2; 5.3] ;; let badlist = [3; 4; 3.14159] ;; (* Problem 2 *) [2;3;4;6];; [2,3 ; 4,5 ; 6,7];; [2.3, 4; 3.2, 5; 6, 7.2];; [["hi"; "there"]; ["how"]; []; ["goezit"]];; (* Adding to lists *) let l1 = [3;6;9];; let l2 = [4;7;10];; 5 :: l1;; 10 :: 20 :: l2;; l1 @ l2;; (* Pattern Matching with Lists *) let getfirst l = match l with | [] -> 0 | x::xs -> x;; getfirst [];; getfirst [3;4;5];; (* Polymorphic List Functions *) let isempty l = match l with | [] -> "empty" | _ -> "nonempty";; isempty ["hi";"there"];; isempty [2;3];; (* Combining Types *) let e1 = [ [20;30]; [10;5;2]; [3;6]; []];; let e2 = [2,3; 4,5; 6,7];; (* If *) if (3 < 5) then print_string "hi!\n" let x = 10;; let y = (if x < 10 then 40 else 50) * 2 ;; (* Sequencing *) (print_string "Bye\n"; 25);; let a = 3 let b = a + 2;;