From 2b739215ec93ab1f4845466555068cdbff756e21 Mon Sep 17 00:00:00 2001 From: gabesw <37423978+gabesw@users.noreply.github.com> Date: Wed, 4 Oct 2023 20:19:56 -0400 Subject: [PATCH] added map2 solution added a solution for map2 that makes use of partial application of functions --- review-problems-sol.ml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/review-problems-sol.ml b/review-problems-sol.ml index 0ddef3f..ea9520f 100644 --- a/review-problems-sol.ml +++ b/review-problems-sol.ml @@ -207,6 +207,15 @@ let rec combine l1 l2 = match l1, l2 with let map2 f l1 l2 = let tup = combine l1 l2 in map (fun (x,y) -> f x y) tup + +let map2 f l1 l2 = + let partial lst1 = match lst1 with + | Nil -> Nil + | xs -> map (fun x -> f x) xs (* use map to partially apply f to each item of l1 *) + in let rec final combinedlst = match combinedlst with + | Nil -> Nil + | Cons ((prtl, x), pxs) -> Cons (prtl x, final pxs) (* now finish applying the partially applied f with each item (x) of l2 *) + in final (combine (partial l1) (l2)) let map2' f l1 l2 = fold_right (fun x1 acc -> (fun l2' -> begin