softshipper
softshipper

Reputation: 34081

Do data constructors support currying?

Everything in Haskell are functions like:

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

The Sentence is a datatype but why it shows as a function? Even when I do substitute with a value, then it feels like a function.

s1 = Sentence "dogs" "drool"  

Does datatype support currying too?

Upvotes: 1

Views: 213

Answers (2)

Rufflewind
Rufflewind

Reputation: 8956

As Jokester noted, confusingly, there are two things both named “Sentence” here:

  • Sentence the type, and
  • Sentence the data constructor.

Many data constructors are functions, because many data types store some stuff inside, and the only way they can do that is by asking for that stuff during construction.

However, objects that have the Sentence type are not functions. They are just ordinary values:

:t (Sentence "he" "likes" "cake")
:: Sentence

Upvotes: 7

Jokester
Jokester

Reputation: 5617

     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)

So the answer is yes, currying applies to the "value constructor" function as well.

Upvotes: 2

Related Questions