
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? I ended up defining my own variation on foldr1 to solve this (code below), but I'd be much happier using some more standard solution: -- A variation on foldr1 that takes an extra argument to be returned if -- the list is empty. foldrs :: (a -> a -> a) -> a -> [a] -> a foldrs f z [] = z foldrs f _ xs = foldr1 f xs -- 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? If there isn't a standard solution, perhaps something like foldrs would be a useful addition to the Prelude. Thanks, -antony -- Antony Courtney Grad. Student, Dept. of Computer Science, Yale University antony@apocalypse.org http://www.apocalypse.org/pub/u/antony

Antony Courtney
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

Antony Courtney
-- 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

Ketil Z. Malde wrote:
Antony Courtney
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?
Yep, that'll do the trick. Thanks for all the lightnin-fast responses. Appologies for overlooking this! -antony -- Antony Courtney Grad. Student, Dept. of Computer Science, Yale University antony@apocalypse.org http://www.apocalypse.org/pub/u/antony

On Fri, 8 Aug 2003, Antony Courtney wrote:
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?
I think the primary intended use for `intersperse' from the List library is to be used as in import List mkSepStr = concat . intersperse ", " HTH, ___cheers,_dave_________________________________________________________ www.cs.bris.ac.uk/~tweed/ | `It's no good going home to practise email:tweed@cs.bris.ac.uk | a Special Outdoor Song which Has To Be work tel:(0117) 954-5250 | Sung In The Snow' -- Winnie the Pooh
participants (4)
-
Antony Courtney
-
D. Tweed
-
ketil@ii.uib.no
-
Malcolm Wallace