2017-03-09 51 views
0

一切都在Haskell是一样的功能:数据构造函数是否支持currying?

Prelude> type Subject = String 
Prelude> type Verb = String 
Prelude> type Object = String 
Prelude> data Sentence = Sentence Subject Verb Object deriving (Eq, Show) 
Prelude> :t Sentence 
Sentence :: Subject -> Verb -> Object -> Sentence 

的句子是一个数据类型,但为什么它显示的功能? 即使我用一个值替代,那么它就像一个函数。

s1 = Sentence "dogs" "drool" 

数据类型是否也支持currying呢?

+1

如果你已经尝试了':t s1',你会发现它的类型是'Object - > Sentence'。 – chepner

回答

7

由于jokester的注意,容易混淆,有两件事情都命名为“Sentence”在这里:

  • Sentence类型和
  • Sentence数据构造。

许多数据构造的功能,因为许多数据类型存储里面的一些东西,所以他们能做到这一点的唯一方法是通过询问施工过程中的东西。

但是,具有Sentence类型的对象不是函数。它们只是普通的值:

:t (Sentence "he" "likes" "cake") 
:: Sentence 
2
 v this is name of a new type 
data Sentence = Sentence Subject Verb Object 
       ^and this is a function called "value constructor" 
        (it may or may not have same name with the new type) 

所以答案是肯定的,currying也适用于“值构造函数”函数。

相关问题