I am a complete Haskell n00b, but I would like to define a new data type that is simple a list of numbers. How would I go about doing this? I've read Haskell wikibook on type declarations, as well as other online resources, but I cannot seem to figure it out. Here is, in essence, what I've tried:
type NumList = [Num]
That didn't work, so how can I do this? Thanks for the help.
The type
keyword is just for type synonyms (new names of types that already exist), so you can't use a class like Num
.
Instead, you might use the data
keyword together with Haskell's context notation:
data Num a => NumList a = NumList [a]
Except when I try that in ghci, it scolds me because datatype contexts are deprecated. Apparently you're better off using a GADT. Perhaps something like:
data NumList a where
Empty :: Num a => NumList a
Singleton :: Num a => a -> NumList a
Append :: Num a => NumList a -> NumList a -> NumList a
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments