Language.Haskell and strings

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Language.Haskell and strings

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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Language.Haskell and strings

Sukit Tretriluxana
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 <[hidden email]> 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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Language.Haskell and strings

Maurí­cio
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 <[hidden email]
> <mailto:[hidden email]>> 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
>     [hidden email] <mailto:[hidden email]>
>     http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Language.Haskell and strings

sclv
In reply to this post by Sukit Tretriluxana
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  
> <[hidden email]> 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\

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe