[Haskell-beginners] Reversing a list

Frederic Cogny frederic.cogny at gmail.com
Tue Jul 4 10:17:43 UTC 2017


Looks like you're missing the square brackets around your x in the
definition of reverse (the ++ takes two lists, hence the error message)

Try this

palindrome :: [t] -> [t]
palindrome xs = xs ++ (reverse xs)
  where
    reverse []     = []
    reverse (x:xs) = (reverse xs) ++ *[*x*]*


to declare the type you can define it outside or annotate it within you're
code

palindrome :: [t] -> [t]
palindrome xs = xs ++ (reverse xs)
  where
   * reverse :: [t] -> [t]*
    reverse []     = []
    reverse (x:xs) = (reverse xs) ++ *[*x*]*

On Tue, Jul 4, 2017 at 12:07 PM Jona Ekenberg <saikyun at gmail.com> wrote:

> 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
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-- 
Frederic Cogny
+33 7 83 12 61 69
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170704/57d2bc88/attachment.html>


More information about the Beginners mailing list