2017-03-09 27 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也適用於「值構造函數」函數。

相關問題