[Haskell-cafe] function unique

Brent Yorgey 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
> stack
> 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
> there
> 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
can do

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
right direction.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070711/e543b38f/attachment.htm

More information about the Haskell-Cafe mailing list