(* DON'T LOOK AT THIS FILE UNTIL AFTER PRECEPT -- THESE ARE SOME SOLUTIONS TO * PRACTICE PROBLEMS YOU'LL TACKLE WITH YOUR TA *) (* equivalent to List.fold_right in the List module *) let rec fold_right (f:'a -> 'b -> 'b) (xs:'a list) (base:'b) : 'b = match xs with [] -> base | hd::tail -> f hd (fold_right f tail base) ;; (* reduce from lecture is the same as fold_right with the arguments flipped *) let reduce (f:'a -> 'b -> 'b) (base:'b) (xs:'a list) : 'b = fold_right f xs base;; (* equivalent to List.fold_left in the List module *) (* more efficient than fold_right (tail recursive) *) let rec fold_left (f:'b -> 'a -> 'b) (base:'b) (xs:'a list) : 'b = match xs with [] -> base | hd::tail -> fold_left f (f base hd) tail ;; (* Simple tests *) let eg1 = [1;2;3;4];; let eg2 = [];; (* Challenges *) (* takes a list and returns the same list: use a fold *) let id xs = fold_right (fun x acc -> x::acc) xs [];; (* takes a list and returns the reverse: use a fold *) let reverse xs = fold_left (fun acc x -> x::acc) [] xs;; let reverse' xs = List.fold_left (fun acc x -> x::acc) [] xs;; (* write the polymorphic map from lecture using fold *) let map f xs = fold_right (fun x acc -> f x::acc) xs [];; (* write a function that maps f across a list and reverses the result *) let map_rev f xs = fold_left (fun acc x -> f x::acc) [] xs;; (* write a function that flattens a list of lists in to a single * list with all of the elements in order. eg: * * flatten [[1;2;3]; []; [4]; [5;6]] == [1;2;3;4;5;6] * * Write the function 2 ways: * 1. with recurseion * 2. without * *) (* with recursion *) let rec concat xs ys = match xs with [] -> ys | hd::tail -> hd::concat tail ys ;; let rec flatten (xss:'a list list) : 'a list = match xss with [] -> [] | xs::tail -> concat xs (flatten tail) ;; (* without recursion *) let concatF (xs:'a list) (ys:'a list): 'a list = fold_right (fun x ys -> x::ys) xs ys ;; let flattenF (xss:'a list list) : 'a list = fold_right concatF xss [] ;; let t1 = [[1;2;3]; [4;5]];;