let swapInt ((x : int), (y : int)) : int * int = (y, x)
let swapReal ((x : float), (y : float)) : float * float = (y, x)
let swapString ((x : string), (y : string)) : string * string = (y, x)
let swapIntReal ((x : int), (y : float)) : float * int = (y, x)
let swapRealInt ((x : float), (y : int)) : int * float = (y, x)
let swap ((x : 'a), (y : 'b)) : 'b * 'a = (y, x)
let appendToString ((x : 'a), (s : string), (convert : 'a -> string)) : string =
(convert x) ^ " " ^ s;;
appendToString (3110, "class", string_of_int);;
appendToString ("ten", "twelve", fun (s : string) -> s ^ " past");;
type answer = Yes | No | Maybe
type eitherPoint = TwoD of float * float
| ThreeD of float * float * float
let lastTwoComponents (p : eitherPoint) : float * float =
match p with
TwoD (x, y) -> (x, y)
| ThreeD (x, y, z) -> (y, z)
type intlist = Nil | Cons of (int * intlist)
let list1 = Nil
let list2 = Cons (1, Nil)
let list3 = Cons (2, Cons(1,Nil))
let list4 = Cons (2, list2)
let list5 = Cons (1, Cons (2, Cons (3, Cons (4, Cons (5, Nil)))))
let list6 = Cons (6, Cons (7, Cons (8, Cons (9, Cons (10, Nil)))))
let rec length(lst: intlist): int =
match lst with
Nil -> 0
| Cons(h,t) -> length(t) + 1
let is_empty(xs:intlist):bool =
match xs with
Nil -> true
| Cons(_,_) -> false
let inc(x:int):int = x + 1
let square(x:int):int = x * x
let rec addone_to_all(list:intlist):intlist =
match list with
Nil -> Nil
| Cons(hd,tl) -> Cons(inc(hd), addone_to_all(tl))
let rec square_all(list:intlist):intlist =
match list with
Nil -> Nil
| Cons(hd,tl) -> Cons(square(hd), square_all(tl))
let rec do_function_to_all((f:int->int), (list:intlist)):intlist =
match list with
Nil -> Nil
| Cons(hd,tl) -> Cons(f(hd), do_function_to_all(f,tl))
let addone_to_all(list:intlist):intlist =
do_function_to_all(inc, list)
let square_all(list:intlist):intlist =
do_function_to_all(square, list)
let addone_to_all(list:intlist):intlist =
do_function_to_all((fun(x) -> x+1), list)
let square_all(list:intlist):intlist =
do_function_to_all((fun(x) -> x*x), list)
let rec sum(list:intlist):int =
match list with
Nil -> 0
| Cons(hd,tl) -> hd + sum(tl)
let rec product(list:intlist):int =
match list with
Nil -> 1
| Cons(hd,tl) -> hd * product(tl)
let rec collapse((f:(int * int) -> int), (b:int), (list:intlist)):int =
match list with
Nil -> b
| Cons(hd,tl) -> f(hd,collapse(f,b,tl))
let sum(list:intlist):int =
let add((i1:int),(i2:int)):int = i1 + i2
in
collapse(add,0,list)
let product(list:intlist):int =
let mul((i1:int),(i2:int)):int = i1 * i2
in
collapse(mul,1,list)
let sum(list:intlist):int =
collapse((fun (i1,i2) -> i1+i2),0,list)
let product(list:intlist):int =
collapse((fun (i1,i2) -> i1*i2),1,list)
type inttree = Empty | Node of node
and node = { value: int; left: inttree; right: inttree }
let rec search ((t: inttree), (x:int)): bool =
match t with
Empty -> false
| Node {value=v; left=l; right=r} ->
v = x || search (l, x) || search (r, x)
type 'a list_ = Nil_ | Cons_ of ('a * 'a list_)
let rec mymap (f: 'a->'b) (x: 'a list_): 'b list_ =
match x with
Nil_ -> Nil_
| Cons_(h,t) -> Cons_(f(h), mymap f t)