# Reversing a list

5 messages
Open this post in threaded view
|

## Reversing a list

 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) ++ xBut 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 [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Reversing a list

 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 codepalindrome :: [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 <[hidden email]> 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) ++ xBut 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 [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -- Frederic Cogny+33 7 83 12 61 69 _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Reversing a list

 In reply to this post by Jona Ekenberg On Tue, Jul 04, 2017 at 12:06:39PM +0200, Jona Ekenberg 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 Hello Jona,     let's analyse the error. It points to this bit:     palindrome xs = xs ++ (reverse xs) And it says: I expected [[t]], but you gave me [t]. Whenever I encounter such an error I try to write explicit type signatures so to make diagnosing easier. In your example     palindrome :: [t] -> [t]     palindrome xs = xs ++ (reverse xs)       where           reverse :: [t] -> [t] -- explicit type signature           reverse []     = []           reverse (x:xs) = (reverse xs) ++ x If we :reload ghci complains again, the offending bit being     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 [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners