idiom for producing comma-seperated lists?

Ketil Z. Malde ketil@ii.uib.no
08 Aug 2003 15:56:18 +0200


Antony Courtney <antony@apocalypse.org> writes:

> -- Example: format a list of strings, using a comma as a seperator:
> mkSepStr :: [String] -> String
> mkSepStr xs = foldrs (\x s -> x ++ ", " ++ s) "" xs
> 
> t0 = mkSepStr []                   -- ==> ""
> t1 = mkSepStr ["hello"]            -- ==> "hello"
> t2 = mkSepStr ["10","20","30"]     -- ==> "10, 20, 30"
> 
> What do the rest of you do to solve this particular problem?

Uh, concat and intersperse?

    Prelude> concat $ List.intersperse ", " $ []
    ""
    Prelude> concat $ List.intersperse ", " $ ["hello"]
    "hello"
    Prelude> concat $ List.intersperse ", " $ ["10","20","30"]
    "10, 20, 30"

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants