(* we started with some basic variable definitions and a simple function *) let x = 3 let y = x * 7 + 2 let f(n) = 2 * n (* we don't generally specify types because OCaml does type inferencing *) let inc(n) = n + 1 (* int version because of + and 1) let inc(n) = n +. 1. (* float version because of +. and 1.) (* I pointed out that sometimes OCaml doesn't know the specific type *) let min(x, y) = if x < y then x else y (* You can specify a specific type in lots of different ways *) let min((x:int), y) = if x < y then x else y let min(x, (y:int)) = if x < y then x else y let min(x, y) : int = if x < y then x else y let min(x, y) = if x < y then (x:int) else y (* Then we defined a recursive function which required "let rec" *) let rec factorial(n) = if n = 0 then 1 else n * factorial(n - 1) (* example of building up a list with the :: operator known as "cons" *) let lst = 45::2::5::7::[] (* recursive function to find the sum of a list of int *) let rec sum(lst) = if lst = [] then 0 else List.hd(lst) + sum(List.tl(lst)) (* recursive function to produce a new list with each value incremented *) let rec inc_all(lst) = if lst = [] then [] else (List.hd(lst) + 1)::inc_all(List.tl(lst)) (* recursive function to return the last value in a nonempty list *) let rec last(lst) = if List.length(lst) = 1 then List.hd(lst) else last(List.tl(lst)) (* data in the form of tuples with last name and first name *) let test = [("Clinton", "Hillary"); ("Obama", "Barak"); ("Biden", "Joe")] (* helper function to convert a tuple to a name in first/last format *) let combine(last, first) = first ^ " " ^ last (* recursive function to return new list with string tuples converted *) let rec convert(lst) = if lst = [] then [] else combine(List.hd(lst))::convert(List.tl(lst))