I am trying to create a backward list using Haskell’s recursive types
data RevList a = Snoc a (RevList a) | Lin
deriving Show
mkrevlst [] = Lin
mkrevlst (x:xs) = mkrevlst xs Snoc x
When I do > mkrevlst [1,2,3] ,the output I am expecting is : ((Lin Snoc 3) Snoc 2) Snoc 1
When I run this I get an error. I am new to Haskell & I am not able to make out where is mistake is.
Where am I going wrong?
Thank you.
I’m not sure what this line was supposed to be, but it doesn’t make sense as is:
The expression
mkrevlist xspresumably has typeRevList a, since the base case above returnsLin. Applying this to two more arguments will indeed result in a type error.It looks like you’re expecting
Snocto be used infix, is that correct? In Haskell, identifiers made of alphanumeric characters are prefix, unless surrounded by backticks, e.g.mkrevlist xs `Snoc` x. Identifiers made of symbols are infix, unless surrounded in parentheses, and infix data constructors specifically must start with a colon. So you could also define your data type like this:Also, note that even if you do use
Snocinfix, the order of its arguments are still backwards from how you’re using it inmkrevlist.