【问题标题】:OCaml: Check a list of records for membershipOCaml:检查成员记录列表
【发布时间】:2009-09-14 01:43:27
【问题描述】:

如果我定义了以下类型:

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

回答“类型转换列表中start = Noun的记录在哪里?”的最佳方法是什么?

有点像

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

除了语法似乎不起作用。

【问题讨论】:

    标签: list types ocaml record


    【解决方案1】:
    List.exists (fun x -> x.start = Noun) un
    

    List.mem 可以认为是List.exists 的一个特例,其中List.mem x ys 等价于List.exists ((=) x) ys。因此,您可以使用List.exists 了解更一般的会员标准。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多