[Haskell-beginners] Reversing a list
Jona Ekenberg
saikyun at gmail.com
Tue Jul 4 10:06:39 UTC 2017
Hello,
I'm currently going through the exercises in chapter 3 of Real World
Haskell. One of the exercises challenges me to create a function which
takes a list and makes a palindrome of it.
I tried to solve it this way:
palindrome :: [t] -> [t]
palindrome xs = xs ++ (reverse xs)
where
reverse [] = []
reverse (x:xs) = (reverse xs) ++ x
But when I try to compile this I get this error:
Kapitel3.hs:14:32-33: error: …
• Couldn't match type ‘t’ with ‘[t]’
‘t’ is a rigid type variable bound by
the type signature for:
palindrome :: forall t. [t] -> [t]
at /Users/jona/programmering/haskell/boken/Kapitel3.hs:13:15
Expected type: [[t]]
Actual type: [t]
• In the first argument of ‘reverse’, namely ‘xs’
In the second argument of ‘(++)’, namely ‘(reverse xs)’
In the expression: xs ++ (reverse xs)
• Relevant bindings include
xs :: [t]
(bound at
/Users/jona/programmering/haskell/boken/Kapitel3.hs:14:12)
palindrome :: [t] -> [t]
(bound at
/Users/jona/programmering/haskell/boken/Kapitel3.hs:14:1)
Compilation failed.
It looks like I have used a function that want a list of lists, but I don't
understand where.
Also, is there some way to declare the type of my reverse-function in this
case?
Kind regards,
Jona Ekenberg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170704/7252097f/attachment.html>
More information about the Beginners
mailing list