idiom for producing comma-seperated lists?

Malcolm Wallace Malcolm.Wallace@cs.york.ac.uk
Fri, 8 Aug 2003 14:55:43 +0100


Antony Courtney <antony@apocalypse.org> writes:

> I often need to format a list of strings using some character as a 
> *seperator* rather than a terminator for the items.  Is there some 
> simple combinator or idiom from the Prelude or standard libraries that 
> could be used for this purpose?

    List.intersperse :: a -> [a] -> [a]

> -- Example: format a list of strings, using a comma as a seperator:
> mkSepStr :: [String] -> String
> mkSepStr xs = foldrs (\x s -> x ++ ", " ++ s) "" xs

    mkSepStr :: [String] -> String
    mkSepStr = concat . intersperse ", "

Regards,
    Malcolm