Nick Heiner
Nick Heiner

Reputation: 122432

OCaml: Check a list of records for membership

If I have defined the following types:

type category = Noun | Verb | Adjective | Preposition;;
type transformation = {start: category; fin: category};;

What is the best way to answer the question "is the record where start = Noun in the list of type transformation?

Sort of like

let un = [{start= Noun; fin= Noun}; {start= Verb; fin= Adjective}];;    
List.mem {start = Noun; _} un;;

Except that syntax doesn't seem to work.

Upvotes: 3

Views: 1615

Answers (1)

newacct
newacct

Reputation: 122429

List.exists (fun x -> x.start = Noun) un

List.mem can be thought of as just a special case of List.exists, where List.mem x ys is equivalent to List.exists ((=) x) ys. So you can use List.exists for more general membership criteria.

Upvotes: 5

Related Questions