(* CS421 - Fall 2009 * MP2 * *) (* Problem 1 *) let reverse_pair (x, y) = (y, x) (* Problem 2 *) let add_times ((x, y), (a, b)) = x * y + a * b (* Problem 3 *) let rev_first_two l = match l with x::x'::xs -> x'::x::xs | _ -> l (* Problem 4 *) let rec concat l = match l with [] -> "" | s::ss -> s ^ concat ss (* Problem 5 *) let rec add_sub l = match l with x::x'::xs -> x - x' + add_sub xs | x::[] -> x | [] -> 0 (* Problem 6 *) let rec accumulate_aux l n = match l with [] -> [] | x::xs -> (x + n)::accumulate_aux xs (x + n) let accumulate l = accumulate_aux l 0 (* Problem 7 *) let rec fixpoint f n = let n' = f n in if n = n' then n else fixpoint f n' (* Problem 8 *) let rec count l a = match l with [] -> 0 | x::xs -> if x = a then 1 + count xs a else count xs a (* Problem 9 *) let rec all p l = match l with [] -> true | x::xs -> p x && all p xs (* Problem 10 *) let rec filter p l = match l with [] -> [] | x::xs -> if p x then x::filter p xs else filter p xs (* Problem 11 *) let rec riffle_aux l1 l2 l2_orig = match l2 with [] -> riffle_aux l1 l2_orig l2_orig | y::ys -> match l1 with x::x'::xs -> x::y::riffle_aux (x'::xs) ys l2_orig | _ -> l1 let riffle l1 l2 = match l2 with [] -> l1 | _ -> riffle_aux l1 l2 l2