English 中文(简体)
OCaml - 配对图中清单参考资料的配对模式
原标题:OCaml - Pattern matching with list reference in a tuple

是否有更清洁的方法这样做? I m 试图做到配对模式

<代码>(备选案文*>)

the only way I found was doing this :

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

Wouldn t there be a way to match a with something else like ...

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

......或某种类似情况? 在这种例子中,它看上去做的是很强的,而实际上它可能有些是......。

Thanks for your answers.

最佳回答

www.un.org/Depts/DGACM/index_french.htm 类型的定义是具有变性领域的记录:

type  a ref = {
    mutable contents :  a;
}

This means that you can pattern match against it using record syntax like this:

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

In OCaml a ref is secretly a record with a mutable field named contents.

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




相关问题
ocamlc, module compilation

I wrote an app in ocaml. It consist of several modules: Util (util.ml) Work1 (work1.ml) -- open Util Work2 (work2.ml) -- open Util, too Main (main.ml) -- open all of them. When i compile its, using ...

How can I simplify this ocaml pattern-matching code?

I m writing a simple little ocaml program that reads an algebraic statement in from a file, parses it into an AST using ocamllex/ocamlyacc, reduces it, and then prints it. The part where I m reducing ...

How can I create a type with multiple parameters in OCaml?

I m trying to create a type that has multiple type parameters. I know how to make a type with one parameter: type a foo = a * int But I need to have two parameters, so that I can parameterize the ...

Hashtable indexed on several fields

I m currently programming an OCaml module defining a type corresponding to a CPU register. The interface of this module is the following : (* * Defines a type which represents a R3000 register. *) ...

Extending an existing type in OCaml

I ve been doing some OCaml programming lately to learn the language and to get more acquainted with functional programming. Recently, I ve started to think that I d like to be able to extend an ...

Ocaml Syntax Error

What s wrong with this code? I can t figure it out: let parent (rules : grammar) (symbol1 : string) (symbol2 : string) : (SymbolSet.t) = try SymbolSet.singleton (getParent [symbol1; symbol2] ...

热门标签