[Haskell-cafe] How to define Show [MyType] ?

Dmitri O.Kondratiev dokondr at gmail.com
Sat Dec 6 18:39:31 EST 2008


Daniel, thanks!
I used your advice, it works, yet some more questions, if you don't mind :)
So:
{--
Why showList returns a function of type:
type ShowS = String -> String
In fact showList returns a function which already contains all values
from list converted to a single string.
Why return function instead of a ready to use string?
--}
data ShipInfo = Ship {
      name :: String,
      kind :: String,
      cannons :: Int
}

s1 = Ship {name ="HMS Fly", kind = "sloop", cannons=16}
s2 = Ship {name ="HMS Surprise", kind = "frigate", cannons=42}

fleet = [s1,s2]

instance Show ShipInfo where
    show x = "Ship "++ show (name x) ++" is a " ++ show(kind x) ++ "
with " ++ show (cannons x) ++ " cannons "
    showList xs = showString (foldr (\x y -> x ++ " <*> "++y) [] (map show xs))


{--
Now when I type 'fleet' I get:

*ShowFleet> fleet
Ship "HMS Fly" is a "sloop" with 16 cannons  <*> Ship "HMS Surprise"
is a "frigate" with 42 cannons  <*>
*ShowFleet>

On the other hand evaluating  t2 :
--}

t2 xs = foldr (\x y -> x ++ " <*> "++y) [] (map show xs)

{--
provides:

*ShowFleet> t2 fleet
"Ship \"HMS Fly\" is a \"sloop\" with 16 cannons  <*> Ship \"HMS Surprise\" is a
 \"frigate\" with 42 cannons  <*> "
*ShowFleet>

More questions:
1)Where characters \" come from in this case?
2)How showList function 'removes' characters \" from the output?
What magic goes behind the scenes here?
--}

Thanks!
Dmitri

On 12/6/08, Daniel Fischer <daniel.is.fischer at web.de> wrote:
> Am Samstag, 6. Dezember 2008 00:13 schrieb Dmitri O.Kondratiev:
>>
>> Thanks everybody for your help!
>> I tried to implement showList, but get the same error:
>>
>> {--
>> -- from Prelude:
>> type *ShowS* =
>> String<file:///C:/usr/ghc-6.6.1/doc/html/libraries/base/Prelude.html#t%3ASt
>>ring>->
>> String<file:///C:/usr/ghc-6.6.1/doc/html/libraries/base/Prelude.html#t%3ASt
>>ring> *showList* :: [a] ->
>> ShowS<file:///C:/usr/ghc-6.6.1/doc/html/libraries/base/Prelude.html#t%3ASho
>>wS> --}
>>
>> myShows::ShowS
>> myShows s = s ++ "\n"
>>
>> data ShipInfo = Ship {
>>       name :: String,
>>       kind :: String,
>>       canons :: Int
>> } deriving Show
>
> No, if you derive the Show instance for ShipInfo, it automatically
> implements
> the standard showList, too. You have to do it yourself:
>
> data ShipInfo = Ship {
> 	name :: String,
> 	kind :: String,
> 	canons :: Int
>       }
>
> instance Show ShipInfo where
> 	showsPrec p (Ship name kind canons) = ...
> 	showList xs = showString (unlines $ map show xs)
>
> or whatever you want for showList.
> However, somebody said it before, the Show instance should not be used for
> pretty-printing.
>
>
>>
>> -- I get this error again:
>> instance (Show [ShipInfo]) where
>>     showList [] = myShows []
>>     showList (x:xs)  = myShows "x" ++ showList xs
>>
>>     Illegal instance declaration for `Show [ShipInfo]'
>>         (The instance type must be of form (T a b c)
>>          where T is not a synonym, and a,b,c are distinct type variables)
>>     In the instance declaration for `Show [ShipInfo]'
>> Failed, modules loaded: none.
>>
>> -- What am I doing wrong?
>> Thanks!
>
>


-- 
Haste makes waste - Lose not a moment!
Dmitri O. Kondratiev
dokondr at gmail.com
http://www.geocities.com/dkondr


More information about the Haskell-Cafe mailing list