Language.Haskell and strings

Hi, I'm using Language.Haskell.* and would like to know if it's possible to pretty-print big strings like this: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" into something like this: "aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa" to respect the limit on number of columns. Can you help me? Is it possible to do that? Thanks, Maurício

I'm not a Haskell expert but here the solution to your problem that I can
think of.
import Data.List
prettyStr :: Int -> String -> IO ()
prettyStr maxlen str = do
putStr ("\"" ++ head brokenStr)
mapM_ (\str -> putStr ("\\\n\\" ++ str)) (tail brokenStr)
putStr "\"\n"
where brokenStr = map (snd.unzip) $ groupBy (\_ (i,_) -> i `mod` maxlen
/= 0) $ zip [0..] str
Ed
On Sat, Sep 20, 2008 at 4:14 PM, Maurício
Hi,
I'm using Language.Haskell.* and would like to know if it's possible to pretty-print big strings like this:
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
into something like this:
"aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa"
to respect the limit on number of columns. Can you help me? Is it possible to do that?
Thanks, Maurício
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Ed, thanks for your code. However, I need to use something that fits inside the whole pretty-print engine. I would like to call parseModule in some string of Haskell code and get it nicely typed, with those big strings inside. Best, Maurício
I'm not a Haskell expert but here the solution to your problem that I can think of.
import Data.List
prettyStr :: Int -> String -> IO () prettyStr maxlen str = do putStr ("\"" ++ head brokenStr) mapM_ (\str -> putStr ("\\\n\\" ++ str)) (tail brokenStr) putStr "\"\n" where brokenStr = map (snd.unzip) $ groupBy (\_ (i,_) -> i `mod` maxlen /= 0) $ zip [0..] str
Ed
On Sat, Sep 20, 2008 at 4:14 PM, Maurício
mailto:briqueabraque@yahoo.com> wrote: Hi,
I'm using Language.Haskell.* and would like to know if it's possible to pretty-print big strings like this:
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
into something like this:
"aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa"
to respect the limit on number of columns. Can you help me? Is it possible to do that?
Thanks, Maurício
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org mailto:Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

A quick glance at the code reveals that there's an instance of Pretty like such: instance Pretty HsLiteral where pretty (HsInt i) = integer i pretty (HsChar c) = text (show c) pretty (HsString s) = text (show s) pretty (HsFrac r) = double (fromRational r) That HsString instance is wrong for what you want, but there doesn't seem any way to simply override it. It should, however, be easy enough to add a further postprocessing layer to your already pretty- printed text, looking line by line for those lines that are a) too long and b) contain a very large quoted string. This layer could fairly easily do something reasonable to break the string up. Alternately, you could write a preprocessing layer that walked the source tree directly and chunked all HsStrings over a certain length into "aaaa" ++ "aaaa" ++ "aaaa", etc. At which point, the pretty printer could then decide how to distribute them properly across lines. Neither would be as perfect a solution as integrating with the pretty printer directly, but both would be helpful, and the latter could probably even be a one or two liner with an appropriate use of SYB. --Sterl On Sep 21, 2008, at 5:37 PM, Sukit Tretriluxana wrote:
Data.List
prettyStr :: Int -> String -> IO () prettyStr maxlen str = do putStr ("\"" ++ head brokenStr) mapM_ (\str -> putStr ("\\\n\\" ++ str)) (tail brokenStr) putStr "\"\n" where brokenStr = map (snd.unzip) $ groupBy (\_ (i,_) -> i `mod` maxlen /= 0) $ zip [0..] str
Ed
On Sat, Sep 20, 2008 at 4:14 PM, Maurí cio
wrote: Hi, I'm using Language.Haskell.* and would like to know if it's possible to pretty-print big strings like this:
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
into something like this:
"aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\ \aaaaaaaa\
participants (3)
-
Maurício
-
Sterling Clover
-
Sukit Tretriluxana