[Haskell-cafe] Formated output with ghci

Frank frank at geoinfo.tuwien.ac.at
Sat Jan 14 11:39:26 EST 2006


A 'stupid' question (sort of an embarassing one):

I want to do formated output by converting data types to strings and the
bring them in a formated way together. I use ghci 6.4 and when I want to see
this text then it is shown as a string (i.e. with "... " added around) and
the formating characters in the text (e.g. \n) are quoted and not executed.
(if I apply show to the text string, then the above is done twice!)

This does occur if I use a defined function (something like 'toString' or
'text') and does not occur if I use show (but I have other reasons not to
use show). 

Is there a cure to have output formated with ghci without using show to
individually convert data types into strings?

Here an example output:

text s2
"last id used #2\n\n(#2,EntType {unEntType = \"Point\"})\n(#1,EntType
{unEntType = \"Point
\"})\n\n\n  deleted\nRelX empty\n\nHCons (Region2start \"start of
region\",RelX empty\n) (
HCons (Region2end \"end of region\",RelX empty\n) (HCons (Point2coord
\"Coordinates of Poi
nt\",\n(#2,\n[NV 0.0 100.0])\n(#1,\n[NV 0.0 0.0])\n\n) HNil))"

Wrapping the text string into a data type and then apply show to this data
type gives also the desired result. Is there an easier way?

Help is highly appreciated

module OutputTest where


data X = X Int Int Float    deriving (Show)

x1 = X 3 4 4.5

text :: X -> String
text (X a b c) = show a ++ show b ++ " \n"  ++ show c 

-- example output
-- >text x1
--"34 \n4.5"

data T a = T a         

instance Show a => Show (T a) where
    show (T a) = "Tx " ++ show a
    
tx1 = T x1

instance Show (T X) where
    show (T a) =  text a
    
    -------- works:
-- >tx1
--34 
--4.5



More information about the Haskell-Cafe mailing list