[Haskell-cafe] (no subject)
dubl-u at hotmail.com
Mon Nov 28 23:55:56 CET 2011
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.
> From: aslatter at gmail.com
> Date: Mon, 28 Nov 2011 16:20:54 -0600
> Subject: Re: [Haskell-cafe] (no subject)
> To: dubl-u at hotmail.com
> CC: haskell-cafe at haskell.org
> On Mon, Nov 28, 2011 at 4:12 PM, Willem Obbens <dubl-u at hotmail.com> 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?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe