
Is there some way to include control characters in a Show string so that they take effect when the element is Shown? For example, if I define a String "a\nb" and use putStrLn on it, the \n causes a return, but when I simply return the string from a function, the \n is shown as \n. Is there a way to get the \n to execute when it's embedded in a returned value? Thanks * -- Russ Abbott _____________________________________________* * Professor, Computer Science California State University, Los Angeles Google voice: 424-235-5752 (424-cell-rja) blog: http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ _____________________________________________*

I wasn't at a computer with Haskell when I wrote the earlier message. Here's how it seems to work. File: *data *MyType = MyType *instance *Show MyType *where* show MyType = "a\nb" Load the above file. Then:
MyType -- This does what I want it to do after all. a b
show MyType "a\nb"
putStrLn (show MyType) a b
print MyType a b
*-- Russ Abbott
_____________________________________________*
* Professor, Computer Science
California State University, Los Angeles
Google voice: 424-235-5752 (424-cell-rja)
blog: http://russabbott.blogspot.com/
vita: http://sites.google.com/site/russabbott/
_____________________________________________*
On Tue, Nov 9, 2010 at 9:40 AM, Russ Abbott
Is there some way to include control characters in a Show string so that they take effect when the element is Shown?
For example, if I define a String "a\nb" and use putStrLn on it, the \n causes a return, but when I simply return the string from a function, the \n is shown as \n. Is there a way to get the \n to execute when it's embedded in a returned value?
Thanks * -- Russ Abbott _____________________________________________* * Professor, Computer Science California State University, Los Angeles
Google voice: 424-235-5752 (424-cell-rja) blog: http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ _____________________________________________*

On Tue, Nov 09, 2010 at 11:44:42AM -0800, Russ Abbott wrote:
I wasn't at a computer with Haskell when I wrote the earlier message. Here's how it seems to work.
File:
*data *MyType = MyType
*instance *Show MyType *where* show MyType = "a\nb"
There's nothing too magical going on here. Remember that show just converts a value to a String. How that String is then displayed depends on what you do with it, not with the definition of show.
Load the above file. Then:
MyType -- This does what I want it to do after all. a b
ghci essentially does "putStrLn . show" on the results of expressions typed at the prompt. So the call to show results in the string consisting of an a, a newline, and a b, and putStrLn prints that string to the screen (with the newline interpreted appropriately).
show MyType "a\nb"
'show MyType' is an expression of type String. Doing putStrLn . show on it causes show to be called on the string, which converts it to a form surrounded by quotes and with control characters escaped. The resulting string is then printed to the screen.
putStrLn (show MyType) a b
This is the same as the first example, since IO actions entered at the ghci prompt are simply run.
print MyType a b
This is also the same, since print = putStrLn . show.
participants (2)
-
Brent Yorgey
-
Russ Abbott