OCaml Language Overview handout #2 Data Type Example Operators/Functions ------------------------------------------------------------------------------- integers int 42 + - * / mod abs reals float 17.5 +. -. *. /. abs_float booleans bool true && || not < > <= >= = <> strings string "hello" ^ String.length String.sub s.[i] characters char 'a' lists list of t [17; 2; 5] List.hd List.tl List.length :: @ tuples t1 * t2... (12, "abc") type conversion functions: <x>_of_<y> where <x> and <y> are int, float, string float_of_int(42) comments: (* this is a comment *) variable binding: let <identifier> = <expression> let x = 2 + 3 * 5 conditional: if <boolean expression> then <expression> else <expression> if x < y then x else y simple function let <name>(<parameters>) = <expression> let sum(a, b) = a + b recursive function let rec <name>(<parameters>) = <expression> let rec factorial(n) = if n = 0 then 1 else n * factorial(n - 1)
Stuart Reges
Last modified: Wed Jan 8 08:06:45 PST 2025