View Problem
Find the common items in two lists
Given two lists, find the common items. E.g. given beans =
Submit a new solution for ocaml
There are 19 other solutions in additional languages (clojure, cpp, csharp, erlang ...)
['broad', 'mung', 'black', 'red', 'white'] and colors = ['black', 'red', 'blue', 'green'], what are the bean varieties that are also color names?
ocaml
(* using standard (functorized) sets *)
module SetTools(ASet: Set.S) =
struct
let of_list l =
List.fold_left (fun acc e -> ASet.add e acc) ASet.empty l
let find_common l1 l2 =
ASet.elements (ASet.inter (of_list l1) (of_list l2))
end
module StringSet = Set.Make(String)
module StringSetTools = SetTools(StringSet)
;;
let beans = ["broad"; "mung"; "black"; "red"; "white"] in
let colors = ["black"; "red"; "blue"; "green"] in
StringSetTools.find_common beans colors;;
module SetTools(ASet: Set.S) =
struct
let of_list l =
List.fold_left (fun acc e -> ASet.add e acc) ASet.empty l
let find_common l1 l2 =
ASet.elements (ASet.inter (of_list l1) (of_list l2))
end
module StringSet = Set.Make(String)
module StringSetTools = SetTools(StringSet)
;;
let beans = ["broad"; "mung"; "black"; "red"; "white"] in
let colors = ["black"; "red"; "blue"; "green"] in
StringSetTools.find_common beans colors;;
Submit a new solution for ocaml
There are 19 other solutions in additional languages (clojure, cpp, csharp, erlang ...)


