[英]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? 回答问题的最佳方法是“在类型转换列表中, start = Noun
的记录是什么?
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. 除了语法似乎不起作用。
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
. List.mem
可以被认为是一种特殊情况的List.exists
,其中List.mem x ys
相当于List.exists ((=) x) ys
。 So you can use List.exists
for more general membership criteria. 因此,您可以将List.exists
用于更一般的成员资格条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.