【问题标题】:OCaml - Pattern matching with list reference in a tupleOCaml - 与元组中的列表引用进行模式匹配
【发布时间】:2012-04-25 03:47:36
【问题描述】:

有没有更清洁的方法来做到这一点?我正在尝试对a进行模式匹配

(a' option * (char * nodeType) list ref

我发现的唯一方法就是这样做:

match a with
| _, l -> match !l with
  | (c, n)::t -> doSomething 

难道没有办法将a 与其他类似的东西相匹配...

match a with
| _, ref (c,n)::t -> doSomething

...或类似的东西?在这个例子中,仅仅做另一场比赛看起来并不重,但在实际情况下,它可能有点......

感谢您的回答。

【问题讨论】:

    标签: functional-programming pattern-matching ocaml ml


    【解决方案1】:

    ref 类型被定义为具有可变字段的记录:

    type 'a ref = {
        mutable contents : 'a;
    }
    

    这意味着您可以使用如下记录语法对其进行模式匹配:

    match a with
    | _, { contents = (c,n)::t } -> doSomething
    

    【讨论】:

    • (哇,我们完全同步了。)
    【解决方案2】:

    在 OCaml 中,ref 是一个秘密记录,其中包含一个名为 contents 的可变字段。

    match a with
    | _, { contents = (c, n) :: t } -> (* Do something *)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多