I was wondering if it is possible to do something like this in Haskell
data Word = Det String | Adj String | Noun String | Verb String | Adverb String
data NounPhrase = Noun | Det Noun
If I’m going about this wrong, what I am trying to say is – a “Word” is either a “Det”, “Adj”, “Noun”, etc. and a “NounPhrase” is a “Noun” OR a “Det” followed by a “Noun”.
When I try to do this I get the error: “Undefined type constructor “Noun””
How can I go about this so it performs as stated above.
You are confusing types and value constructors. When you say
You define the type
Wordas being one of a number of forms, for examplesays that
Detis a constructor that takes aStringand gives you back a value of typeWord. Same goes withNounwhich is defined to be a constructor for Words and not a type.There are various ways you might encode what you want in Haskell. By far the simplest is to use
and this is the “learning Haskell” way of doing it. It is “stringly typed”, but is probably sufficient for your purposes.