[Haskell-cafe] function unique
byorgey at gmail.com
Wed Jul 11 16:11:25 EDT 2007
On 7/11/07, Alexteslin <alexteslin at yahoo.co.uk> wrote:
> Oh, I am lost now - for now anyway.
> I am attempting to do next exercise in the book to define reverse function
> using primitive recursion and pattern matching on lists. But getting
> because when i con in front of xs (xs:x) i get en error, which i thought i
> would be getting anyway. I tried to define a helper function and cons
> in front of xs and i get type errors again.
> I know these are easy and boring questions but i would appreciate a hint.
> Thank you
Let's look at the type of the cons operator:
Prelude> :t (:)
(:) :: a -> [a] -> [a]
That is, it takes an element of any type (here represented by 'a'), and a
list of the same type, and produces a list. So (xs:x) does not make sense,
assuming that xs is a list and x has the same type as an element of xs; cons
expects an element followed by a list, and you are giving it a list followed
by an element. If you want to append an element onto the end of a list you
xs ++ [x]
++ is the list append operator, so this says "make x into a list with one
element, and append it onto the end of the list xs".
Does that help? It's hard to help without seeing any code or the specific
errors you are seeing, but hopefully that should give you a push in the
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe