2014-02-09 79 views
3

type t1 = A of int * stringtype t2 = A of (int * string),它们是不同的还是相同的?OCaml中的这两个类型定义有什么区别

在这种functional programming tutorial,滑动件6,它说

在OCaml中,变体采取多个参数而不是把元组 作为参数:INT *串的A是二FF erent比A(INT *字符串)。 但是这并不重要,除非你被它咬了。

但我除了一对括号之外没有看到任何区别。

+1

这些是两种不同的类型。这已经在SO上被多次询问和回答。这里是例子:http://stackoverflow.com/questions/14818866/int-int-vs-int-int-in-ocaml-sum-type/14819463#14819463 http://stackoverflow.com/questions/10306733/ocaml -constructor-拆包/ 10306865#10306865 –

回答

0

尝试以下方法:

type t1 = A of int*int 
type t2 = B of (int*int) 
let x = (1,2) in A x (* does not work *) 
let x = (1,2) in B x (* works *) 

即,B是一个构造期望1个参数(即含两个整数的元组),而A是一个构造接受2个参数(如带括号的供给,逗号分开的东西)。