Re: [Haskell-cafe] Separate a string into a list of strings

Quoth Clifford Beshers

Quoth Clifford Beshers
: | Well, I couldn't resist the puzzle. Here are solutions using foldr and | unfoldr. Don't know if they are cunning or not, but they were kind of fun. ... | splitByElem1 e xs = | foldr f [[]] xs | where f a b = if a == e then [] : b else (a : head b) : (tail b)
This does the right thing with trailing separators, which is not to be taken for granted among Haskell split implementations. The splits I have been seeing in this thread are conservative, so if the separator is ':', then "::a" splits to ["", "", "a"]. Frequently however the implementation fails to deal with the trailing separator, so "a:" is ["a"], where it should be ["a", ""]. It's not something you run into right away.
In a liberal split, "a " should indeed be ["a"], but that's a different matter. Neither of the two I've looked at seems to be shooting for a liberal "words" white space split. Good point. My solutions are inconsistent on white space, which I don't
Donn Cave wrote: like. Your criterion eliminates this solution as well: filter (/= ",") $ groupBy (\x y -> x /= ',' && y /= ',') "Haskell, Haskell, and Haskell," ["Haskell"," Haskell"," and Haskell"]
participants (2)
-
Clifford Beshers
-
Donn Cave