let rec sum n = if n = 0 then 0 else n + sum (n - 1) let goodSum n = let rec helper acc n = if n = 0 then acc else helper (n + acc) (n - 1) in helper 0 n helper 0 4 ==> helper (4 + 0) (4 - 1) ==> helper 4 3 ==> helper (3 + 4) 2 ==> helper 7 2 ==> helper (2 + 7) 1 ==> helper 9 1 ==> helper (1 + 9) 0 ==> helper 10 0 ==> 10 let doTwice (f : 'b) (x : 'a) = f (f x) 1. f must be a function So: 'b = 'c -> 'd 2. f is taking x as input So: 'c = 'a doTwice (f : 'a -> 'd) (x : 'a) = f (f x) ----- 3. f is itself taking f x as input --- : 'd 4. So, 'a = 'd doTwice (f : 'a -> 'a) (x : 'a) : 'a = f (f x) doTwice : ('a -> 'a) -> 'a -> 'a ====== HW1 due on Friday next week, 10pm. Quiz 1 moved by one week: Tuesday, October 8.