[Haskell-cafe] Multi Line String literals

Bas van Dijk v.dijk.bas at gmail.com
Fri Apr 27 03:02:03 EDT 2007

On 4/26/07, Joe Thornber <joe.thornber at gmail.com> wrote:
> but it's simpler to just write something like:
> test  = putStr $ unlines ["I",
>                                    "need",
>                                    "multiline",
>                                    "string",
>                                    "literals"]

Yes I know. My aim was just to see if I could do it with as less
syntax as possible and then the 'do' syntax is handy.

Another question:

If I change:

type MLS = Writer (Endo [B.ByteString]) ()

instance IsString MLS where
    fromString s = tell $ Endo (fromString s:)

instance ToString MLS where
    toString w = toString $ B.unlines $ (appEndo $ execWriter w)  []

to the simpler:

type MLS2 = Writer [B.ByteString] ()

instance IsString MLS2 where
    fromString s = tell [fromString s]

instance ToString MLS2 where
    toString = toString . B.unlines . execWriter

then MLS2 is a bit faster (about 0.4 sec. when applied to a million
strings). I thought the former MLS should be faster because it doesn't
have to mappend every [fromString s]. MLS just 'builds' a large
function: "(fromString s:) . (fromString s:) . (fromString s:) ... id

Can anybody explain why MLS2 is faster?


Bas van Dijk

More information about the Haskell-Cafe mailing list