On Tue, Jul 04, 2017 at 12:06:39PM +0200, Jona Ekenberg wrote:Hello Jona,
> 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
let's analyse the error.
It points to this bit:
And it says: I expected [[t]], but you gave me [t]. Whenever I encounter
palindrome xs = xs ++ (reverse xs)
such an error I try to write explicit type signatures so to make diagnosing
easier. In your example
reverse :: [t] -> [t] -- explicit type signature
palindrome :: [t] -> [t]
palindrome xs = xs ++ (reverse xs)
where
reverse [] = []If we :reload ghci complains again, the offending bit being
reverse (x:xs) = (reverse xs) ++ x
^
reverse (x:xs) = (reverse xs) ++ x
Expected type is [t1] but we passed t. Not it is clear! The type of `++` is:
λ> :t (++)
(++) :: [a] -> [a] -> [a]
and `x` is a single element. When we replace `x` with `[x]` everything works.
Does that help?
-F
P.S.: Real World Haskell is an excellent book but sometimes can be a tad
difficult to follow. If you want to integrate with another source, CIS194 [1]
is an excellent choice: free, thorough, full of home-works and interactive.
[1] http://www.cis.upenn.edu/~cis194/fall16/
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners