Re: [Haskell-cafe] Formated output with ghci

2006-01-14 Thread Cale Gibbard
On 14/01/06, Frank <[EMAIL PROTECTED]> wrote:
> 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 Floatderiving (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
>

Basically, GHCi and Hugs follow the rule that if the expression you
type is an IO action, then that action gets run, and otherwise, the
function 'print' is applied to it automatically, to print the value.
The print function is the composition of putStrLn with show, and the
show instance for Strings quotes them. The solution is to explicitly
apply putStrLn to your String value rather than letting it be quoted.

> putStrLn $ text x1
34
4.5

 - Cale
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Formated output with ghci

2006-01-14 Thread Frank
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 Floatderiving (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

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe