[Haskell-cafe] need help on exercise

Graham Klyne gk at ninebynine.org
Mon Jun 28 04:38:44 EDT 2004


Your problem seems to be related to the Num class, and the fact that the 
compiler can't figure out what '5' is -- see pp 223/224 of Simon Thompson's 
book.

The code below offers two possible resolutions, one commented:
[[
class Show a => Visible a where
   toString :: a -> String
   size     :: a -> Int

   toString x = show x
   size x = 1

{-
instance Visible Int where
   toString x    = show x
   size _        = 1
-}

instance Num a => Visible a where
   toString x    = show x
   size _        = 1

{-
t1 - toString (5::Int)
-}

t1 = toString 5
]]

#g
--

At 23:58 27/06/04 -0400, kasager wrote:
>Hi,  in my last posting i asked for help with exercise 12.5 however, i forgot
>to give my code... here is my code and the error messege that hugs gives when
>i try to run it (it compiles fine, however when i test it, it gives an
>error)...
>
>Code:
>
>class Show a => Visible a where
>   toString :: a -> String
>   size     :: a -> Int
>
>   toString x = show x
>   size x = 1
>
>instance Visible Int where
>   toString x    = show x
>   size _        = 1
>
>-- i have tried writing this without the show function, and the same error
>messege still is given... i also have other instances in the class, which 
>work
>fine and should not effect this issue...
>
>Error:
>
>Prelude> :l "C:\\WorkArea\\Haskell\\homework\\ch12_5.hs"
>Main> toString 5
>ERROR - Unresolved overloading
>*** Type       : (Num a, Visible a) => [Char]
>*** Expression : toString 5
>
>
>If you can help in any way or give any suggestions its greatly appreciated...
>once again thanks much.

------------
Graham Klyne
For email:
http://www.ninebynine.org/#Contact



More information about the Haskell-Cafe mailing list