What does Cons and :-: mean in Haskell?

data List a = Cons a (List a) deriving (Show, Read, Eq, Ord)

I understand what most of this means apart from Cons. When I try :t Cons and :i Cons in ghci I get a not in scope error.

You need to load the Haskell source file with the data declaration before you can have Cons in scope. Or, alternatively, you can enter that data line directly in GHCi.

For serious code, it’s easier if you put it in a file and load it. This is because the learning process typically involves modifying the file a bit, reloading it, trying some test in GHCi, modifying the file again, etc. Doing this in GHCi is cumbersome.

Anyway, Cons is just the constructor name — it is an arbitrary name. You can use data List a = Foobar a (List a) .... and name it Foobar, if you wish. Cons is a historic name, though, originating from Lisp.

:-: is another arbitrary name for the constructor, except that it can be used infix. I.e. instead of Cons 1 someList one can write 1 :-: someList.

Leave a Comment