Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List.filter and List.mem

I am having a problem understanding this particular line

let lst = ["a", "b", "c"];
List.filter (fun (a, _b) -> not (List.mem a lst)) assoc

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

List.filter takes a function that takes each element in the list in turn and returns a boolean. If the boolean value is true, the element is part of the returned list.

In this case, any tuple in assoc whose first element is not in lst is returned as a new list.

Consider:

let lst = ["a"; "b"; "c"]
let assoc = [("a", 42); ("g", 27)]

let filtered = List.filter (fun (a, _) -> not @@ List.mem a lst) assoc
(* [("g", 27)] *)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading