I'm trying to get used to defining recursive types in Haskell. As a simple exercise, I figured that defining a nonempty list data type would be easy (and potentially useful).
Here is my attempt:
data NonemptyList a = Singleton a | Cons (Singleton a) (NonemptyList a)
which results in the compile error:
Not in scope: type constructor or class `Singleton'
Failed, modules loaded: none.
The following code compiles but doesn't sit well with me. I can't exactly explain why not.
data NonemptyList a = Singleton a | Cons a (NonemptyList a)
Can anyone clarify this for me? Any comments are welcome.
Thanks
dan
Singletonis a data constructor — you use these to define values. When defining types, you use type constructors. These two are different concepts and you can't mix them. All I can say is that the second option (the one that works) is perfectly fine, I don't know why you don't like it. - Paul MantaConsisNonemptyList a;NonemptyList ais a type. - Gabriella Gonzalez