迷彩变体类型 [英] Ocaml Variant Types
问题描述
OCaml类型始终使我感到困惑,无论我阅读了什么教程/手册.我需要定义一个类型,比如说test,它确实包含以下形式:
OCaml types have always really confused me no matter what tutorials/manuals I read. I need to define a type, let's say test, that does contains the following forms:
具有以下形式之一的类型('a,'b)测试: 空,T t,N n,Seq [x1 ... xn]
a type ('a, 'b) test that has one of the forms: Empty, T t, N n, Seq [x1...xn]
我知道怎么做前3个,但是我绝对不知道如何定义最后一个形式.这就是我所拥有的:
I know how to do the first 3, but I have absolutely no idea how to define the last form. This is what I have:
type ('nonterm, 'term) test =
| Empty
| T of 'term
| N of 'nonterm
| ????
对于seq,我需要将子表达式x1的实例匹配为xn.如果x = 0,则为空.有人可以帮我吗?预先感谢.
For seq, I need to match the instances of subexpressions x1 to xn. If x = 0, then this is empty. Can anybody please help me? Thanks in advance.
推荐答案
Seq
的子表达式是否也在测试?如果是这样,您可以使用一个列表:
Are the subexpressions of Seq
also tests? If so, you can use a list:
type ('nonterm, 'term) test =
| Empty
| T of 'term
| N of 'nonterm
| Seq of ('nonterm, 'term) test list
列表当然可以为空.
这篇关于迷彩变体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!