rec02_jb

# rec02_jb - functions let f(x:int(y:int:int = x y let rec...

This preview shows pages 1–3. Sign up to view the full content.

(* functions *) let f((x:int),(y:int)):int = x+y;; let rec fact(n:int):int = if n=0 then 1 else n*fact(n-1);; f(2+3,5*6);; fact(2*2);; (* variables *) let x=1;; let f(y:int):int=x+y;; let x=2;; x;; f(0);; (* tuples *) let max1((r1:float), (r2:float)):float = if r1 < r2 then r2 else r1;; max1(3.1415, 2.718);; (3.1415, 2.718);; (42, "hello", true);; ();; let args = (3.1415, 2.718);; max1 args;; fst (1,2);; let max2(pair:float*float):float = if (fst pair) < (snd pair) then (snd pair) else (fst pair);; max2(3.,4.5);; (* Pattern matching tuples *) let max3(pair:float*float):float = let (r1, r2) = pair in if r1 < r2 then r2 else r1;; let max1((r1:float), (r2:float)):float = if r1 < r2 then r2 else r1;; max1(3.1415, 2.718);; let minmax(a, b):float*float = if a < b then (a, b) else (b, a);; let (mn, mx) = minmax(2.0, 1.0);; (* Records *) type account = {first:string; last:string; age:int; balance:float};;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
let pers1={first = "John"; last = "Doe"; age = 150; balance = 0.12};; pers1.age;; let pers2={last = "Smith"; first = "Joe";
This is the end of the preview. Sign up to access the rest of the document.

### Page1 / 4

rec02_jb - functions let f(x:int(y:int:int = x y let rec...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online