[Haskell-cafe] Using ShowS or Difference Lists

Daniel Fischer daniel.is.fischer at web.de
Sat Feb 6 08:25:56 EST 2010


Am Samstag 06 Februar 2010 13:42:40 schrieb Mark Spezzano:
> Hi,
>
> Just wondering whether I can use ShowS or tupling or Difference Lists to
> speed up the following code?
>
> It's basic text processing. It takes in a list of Lines where each Line
> is a list of Words and intersperses " " between them then concatenates
> them into a longer String. Note that there is a recursive call and the
> ++ operator.
>
> Thanks
>
> Mark
>
>
> -- Function: joinLines
> -- Joins the Words within Lines together with whitespace and newline
> characters -- Argument: Lines to pad with whitespace and newlines
> -- Evaluate: The processed and concatenated String
> joinLines :: [Line] -> String
> joinLines (l:[]) = concat (intersperse " " l)
> joinLines (l:ls) = (concat (intersperse " " l)) ++ ('\n':joinLines ls)
>

joinLines = init . unlines . map unwords
joinLines = concat . intersperse "\n" . map unwords
joinLines = intercalate "\n" . map unwords
joinLines = intercalate "\n" . map (intercalate " ")

it should be pretty good already, if that's a performance bottleneck, you 
might need to switch to (Lazy) ByteStrings. I don't think ShowS or 
difference lists would be any faster.


More information about the Haskell-Cafe mailing list