号如果有一个像签名:
Branch :: (Tree a, Tree a) -> Tree a
那么你应该给它一个元组,并调用类的构造函数:但是
Branch (tree1,tree2)
Branch
有一个签名:
Branch :: Tree a -> Tree a -> Tree a
或带明确的括号:
Branch :: Tree a -> (Tree a -> Tree a)
所以,当你喂Branch
第一参数,如Branch tree1
,签名是:
(Branch t1) :: Tree a -> Tree a
当你终于给它第二个参数t2
,其类型会崩溃到:
((Branch t1) t2) :: Tree a
一个更方便的例子是,例如(+)
whic h不是一个构造函数,而是一个函数。 (+)
我们加(+)
具有签名
(+) :: Int -> Int -> Int
(Haskells加上是更通用一些,但我们不要考虑这个了)。
如果您写信(5+)
你有专门的函数的函数:
(5+) :: Int -> Int
换句话说,你构建的新功能,将增加5
为给定数。
由于签名(a,b) -> c
和a -> b -> c
是却丝毫等同,哈斯克尔提供curry
和uncurry
功能:如果你决定你需要例如用于Branch
这应该是一个额外的构造
curry :: ((a, b) -> c) -> a -> b -> c
uncurry :: (a -> b -> c) -> ((a, b) -> c)
喂一个元组,你可以构造这样的构造函数:
branchTuple :: (Tree a, Tree a) -> Tree a
branchTuple = uncurry Branch
在这两种情况下都有一个_type_参数:'a'。 –