[Haskell-cafe] [] vs [()]
Sam Danielson
samdanielson at gmail.com
Fri Oct 10 01:17:31 EDT 2008
> What is the difference between empty list [] and list with one unit element [()]?
>
The [] constructor takes no arguments and is like Nothing in the Maybe
type. The list ":" (cons) infix constructor takes two arguments, an
element of type a and a list of type a, to construct a new list. Compare
to Maybe.
data [] a = [] | a : [a]
data Maybe a = Nothing | Just a
Another way of saying [()] is
():[]
which, comparing with the Maybe type, is similar to saying
Just ()
but Just only takes one argument where (:) takes two.
Both List and Maybe are containers that have a null constructor, namely
[] and Nothing. "():[]" contains () similar to how "Just ()" contains
(). You can make your own list type and put () in it as follows.
data List a = Empty | Cons a (List a) deriving (Show, Eq)
() `Cons` Empty -- This is ():[]
-Sam
PS. I'm new on the list and also to Haskell.
More information about the Haskell-Cafe
mailing list