(**************************************) (* PART 1: summing a lot of integers *) (**************************************) (* sum of 0..n *) let rec sum (n:int) : int = if n > 0 then n + sum (n-1) else 0 (* summing integers differently *) let sum2 (n: int) : int = let rec aux (n:int) (a:int) : int = if n > 0 then aux (n-1) (a+n) else a in aux n 0 (* testing *) let big_int = 1000000 let win() = sum2 big_int let lose() = sum big_int (*******************************************) (* PART 2: summing a big list of integers *) (*******************************************) (* sum of the contents of l *) let rec sum_list (l:int list) : int = match l with [] -> 0 | hd::tail -> hd + sum_list tail (* sum the list a different way *) let sum_list2 (l:int list) : int = let rec aux (l:int list) (a:int) : int = match l with [] -> a | hd::tail -> aux tail (a+hd) in aux l 0 (* testing *) let make (n:int) : int list = let rec aux (n:int) (l:int list) = if n <= 0 then l else aux (n-1) (n::l) in aux n [] let big_list = make 1000000 let win2() = sum_list2 big_list let lose2() = sum_list big_list