Split a character string based on change of character: Difference between revisions

→‎{{header|Haskell}}: Added a foldr variant
(Split a character string based on change of character en Yabasic)
(→‎{{header|Haskell}}: Added a foldr variant)
Line 1,226:
 
{{Out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
 
or as a hand-written fold:
<lang haskell>import Data.List (intercalate)
import Data.Bool (bool)
 
charGroups :: String -> [String]
charGroups =
let go (a, b) (s, groups)
| a == b = (b : s, groups)
| otherwise =
( [a],
bool s [b] (null s) : groups
)
in uncurry (:) . foldr go ([], []) . (zip <*> tail)
 
main :: IO ()
main =
putStrLn $ intercalate ", " $ charGroups "gHHH5YY++///\\"</lang>
<pre>g, HHH, 5, YY, ++, ///, \</pre>
 
9,655

edits