Tutorial 3 Notes Rebecca Dreezer Functions - fun v1 v2 ... vn -> expression - let identifier v1 v2 ... Vn = expression - OCaml doesn't have a return keyword - the last expression in a function becomes the result of the function automatically. Increment Function: - let increment = fun i -> i + 1;; - let increment = function i -> i + 1;; - let increment i = i + 1;; - val increment: int -> int = Currying - function that takes a single integer argument and returns a function that takes another integer argument Multiply Function: - let multiply a b = a * b;; - multiply 3 4;; - let triple = multiply 3;; triple 4;; Sum Function: - let sum = fun i j -> i + j;; - let sum = (fun i -> (fun j -> i + j));; - val sum: int -> int -> int = - sum 4 5;; Average Function: - let average a b = (a +. b) /. 2.0;; - val average : float -> float -> float = - let average (a,b) = (a +. b) /. 2.0;; - Val average: float * float -> float = Recursive Power Function: - let rec power i x = if i = 0 then 1.0 else x * (power (i – 1) x);; - val power: int -> float -> float = For loop structure: - for i = 1 to 10 do print_int i done;; - for variable = start downto end do expression done ;; List.iter Example: - let f elem = Printf.printf "I'm looking at element %d now\n" elem in List.iter f my_list ;;