数据构造函数支持咖喱



haskell中的所有内容都是:

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"  

数据类型也支持咖喱吗?

正如Jokester所指出的那样,令人困惑的是,这里有两件事都名为" Sentence":

  • Sentence类型,
  • Sentence数据构造函数。

许多数据构造函数是函数,因为许多数据类型都在其中存储了一些东西,而他们可以做到的唯一方法是在施工过程中询问这些东西。

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

:t (Sentence "he" "likes" "cake")
:: Sentence
     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)

所以答案是肯定的,咖喱也适用于"值构造函数"功能。

最新更新