
Hello, I get this error when I try to derive an instance of the Show typeclass: Abc.hs:21:60: Couldn't match expected type `Vector' with actual type `[Point]' In the first argument of `show'', namely `xs' In the second argument of `(++)', namely `show' xs' In the second argument of `(++)', namely `", " ++ show' xs'Failed, modules loaded: none. Here's the faulty code: newtype Point = Point Intinstance Show Point where show (Point a) = [chr $ a + 48] data Vector = Vector [Point]instance Show Vector where show (Vector ys) = let show' (Vector [z]) = show z show' (Vector (x:xs)) = show x ++ ", " ++ show' xs show' (Vector []) = [] in "(" ++ show' ys ++ ")" What I'm trying to do, is to print out a vector like "Vector [Point 1, Point 2, Point 3]" as "(1, 2, 3)".My question is: what should I change in order to make it work? Best regards, William

On Mon, Nov 28, 2011 at 4:12 PM, Willem Obbens
Hello, I get this error when I try to derive an instance of the Show typeclass: Abc.hs:21:60: Couldn't match expected type `Vector' with actual type `[Point]' In the first argument of `show'', namely `xs' In the second argument of `(++)', namely `show' xs' In the second argument of `(++)', namely `", " ++ show' xs' Failed, modules loaded: none. Here's the faulty code: newtype Point = Point Int instance Show Point where show (Point a) = [chr $ a + 48]
data Vector = Vector [Point] instance Show Vector where show (Vector ys) = let show' (Vector [z]) = show z show' (Vector (x:xs)) = show x ++ ", " ++ show' xs show' (Vector []) = [] in "(" ++ show' ys ++ ")"
Here you're treating the value 'ys' as if its type was 'Vector', but its type is '[Point]'. Does that help? Antoine

On Mon, Nov 28, 2011 at 04:20:54PM -0600, Antoine Latter wrote:
On Mon, Nov 28, 2011 at 4:12 PM, Willem Obbens
wrote: Hello, I get this error when I try to derive an instance of the Show typeclass: Abc.hs:21:60: Couldn't match expected type `Vector' with actual type `[Point]' In the first argument of `show'', namely `xs' In the second argument of `(++)', namely `show' xs' In the second argument of `(++)', namely `", " ++ show' xs' Failed, modules loaded: none. Here's the faulty code: newtype Point = Point Int instance Show Point where show (Point a) = [chr $ a + 48]
data Vector = Vector [Point] instance Show Vector where show (Vector ys) = let show' (Vector [z]) = show z show' (Vector (x:xs)) = show x ++ ", " ++ show' xs show' (Vector []) = [] in "(" ++ show' ys ++ ")"
You've made show' :: Vector -> String, but I'm guessing you actually want to make it show' :: [Point] -> String; i.e. get rid of the Vector constructors in the show' patterns. -Brent

Yes, thank you. Here's my simple fix: newtype Point = Point Int instance Show Point where show (Point a) = [chr $ a + 48] data Vector = Vector [Point] instance Show Vector where show (Vector ys) = let show' [z] = show z show' (x:xs) = show x ++ ", " ++ show' xs show' [] = [] in "(" ++ show' ys ++ ")" And I added this function: createPoint :: Int -> PointcreatePoint x = Point x When I loaded the file containing all this into ghci and executed 'Vector $ map createPoint [1..5]' the result was '(1, 2, 3, 4, 5)' (without the quotes).This was actually more or less a test question as I'm new to haskell-cafe, but I hope people who will read this message will learn from my mistake. Thank you.
From: aslatter@gmail.com Date: Mon, 28 Nov 2011 16:20:54 -0600 Subject: Re: [Haskell-cafe] (no subject) To: dubl-u@hotmail.com CC: haskell-cafe@haskell.org
On Mon, Nov 28, 2011 at 4:12 PM, Willem Obbens
wrote: Hello, I get this error when I try to derive an instance of the Show typeclass: Abc.hs:21:60: Couldn't match expected type `Vector' with actual type `[Point]' In the first argument of `show'', namely `xs' In the second argument of `(++)', namely `show' xs' In the second argument of `(++)', namely `", " ++ show' xs' Failed, modules loaded: none. Here's the faulty code: newtype Point = Point Int instance Show Point where show (Point a) = [chr $ a + 48]
data Vector = Vector [Point] instance Show Vector where show (Vector ys) = let show' (Vector [z]) = show z show' (Vector (x:xs)) = show x ++ ", " ++ show' xs show' (Vector []) = [] in "(" ++ show' ys ++ ")"
Here you're treating the value 'ys' as if its type was 'Vector', but its type is '[Point]'.
Does that help?
Antoine

On Mon, Nov 28, 2011 at 23:55, Willem O
And I added this function: createPoint :: Int -> Point createPoint x = Point x When I loaded the file containing all this into ghci and executed 'Vector $ map createPoint [1..5]' the result was '(1, 2, 3, 4, 5)' (without the quotes).
Note that you do not need this function. You can just use the 'Point' constructor: map Point [1..5] Erik
participants (5)
-
Antoine Latter
-
Brent Yorgey
-
Erik Hesselink
-
Willem O
-
Willem Obbens