Problem 1: You're given a list, say [1; 2; 3; 4; 5; 6]. Write a function which returns all even numbers in this list. let rec allEven l = match l with | [] -> [] | hd :: tl -> if hd mod 2 = 0 then hd :: (allEven tl) else allEven tl Problem 2: Given a list, find all numbers which are greater than 50. let rec bigNums l = match l with | [] -> [] | hd :: tl -> if hd > 50 then hd :: (bigNums tl) else bigNums tl Problem 3: Find all primes in a list of numbers. let isPrime n = let rec _isPrime n k = if k >= n then true else if (n mod k = 0) then false else _isPrime n (k + 1) in _isPrime n 2 let rec findPrimes = match l with | [] -> [] | hd :: tl -> if isPrime hd then hd :: findPrimes tl else findPrimes tl let filter pred l = match l | [] -> [] | hd :: tl -> if pred hd then hd :: filter pred tl else filter pred tl ======================================================================================================================== Problem 4: Given a list of numbers, double every number in the list. let rec doubleAll l = match l with | [] -> [] | hd :: tl -> (2 * hd) :: doubleAll tl Problem 5: Given a list of URLS, download all webpages. let downloadURL addr = () let rec downloadAll = function | [] -> [] | hd :: tl -> (downloadURL hd) :: (downloadAll tl) let rec map f l = match l with | [] -> [] | hd :: tl -> (f hd) :: (map f tl) ======================================================================================================================== f : Stepper Problem 6: Banker. Sum all elements of the list. let rec endingBalance l = match l with | [] -> 0., ________ | hd :: tl -> hd +. (endingBalance tl) Problem 7: Find the richest person in the Forbes 500 list. "max of the list" let getPersonalWealth p = ??? let rec richest l = match l with | [] -> ______0, int_min, None________ | hd :: tl -> max (getPersonalWealth hd) (richest tl) Keep running count of (index, net worth) of richest so far. let rtl = richest tl if (getPersonalWealth hd) > (getPersonalWealth rtl) then hd else rtl let rtl = richest tl in match rtl with | None -> hd | Some p -> if (getPErsonalWealth hd) > (getPW p) then hd else p (RichestPerson in the tail, Current person) ===> Richest person in the full list. step (0, max) (None, step) Problem 8: You're in charge of launching the SpaceX rocket Falcon. You receive "good-to-go signals" from all your project managers. Do you launch? let rec goToLaunch l = match l with | [] -> false | [ hd ] -> hd | hd :: tl -> hd && goToLaunch tl let rec goToLaunch l = match l with | [] -> true | hd :: tl -> hd && goToLaunch tl ======================================================================================================================== "real" "float" / "double" / "long double" (Double precision floating point) 1342.68 ===> 1.34268 * 10^3 ===> (1342, 3) 2986423.8735 ===> 2.9864238735 * 10^6 ===> (2986428735, 6) 2986423.8735 ~~~=> 2986424 Are there ways to talk about real numbers which are not "float"? "Fixed precision" 2342832.45 2342832.453 ==> 2342832.45 "Continuous fraction" ======================================================================================================================== How to distinguish fold_left from fold_right. Suggestion: Use a non-commutative operation, say division. utop # List.fold_left;; - : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = fold_left (/.) 1.0 [2.0] =====> 2.0, 0.5 ======================================================================================================================== int accumulator = 0; for (l in list): accumulator = f(accumulator, l) ======================================================================================================================== M's failed suggestion: Init: 2 List: [3; 4] Operation: ** Left-to-right dataflow: (2 ^ 3)^4 = 4096 Right-to-left dataflow: (2^4)^3 == 4096.