Tokenize a string: Difference between revisions

Changed task to include list additionally to array; Haskell example
m (Switched to header template)
(Changed task to include list additionally to array; Haskell example)
Line 1:
{{task}}
Separate the string "Hello,How,Are,You,Today" by commas into an array (or list) so that each indexelement of the arrayit stores a different word. Display the words to the 'user', in the simplest manner possible, separated by a period. To simplify, you may display a trailing period.
 
=={{header|Ada}}==
Line 154:
s" Hello,How,Are,You,Today" s" ," split .tokens \ Hello.How.Are.You.Today
 
=={{header|Haskell}}==
 
The necessary operations are unfortunately not in the standard library (yet), but simple to write:
 
splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy _ [] = []
splitBy f list = first : splitBy f (dropWhile f rest) where
(first, rest) = break f list
joinWith :: [a] -> [[a]] -> [a]
joinWith _ [] = []
joinWith _ [x] = x
joinWith d (x:xs) = x ++ d ++ joinWith d xs
 
putStrLn $ joinWith "." $ splitBy (== ',') $ "Hello,How,Are,You,Today"
 
=={{header|Groovy}}==
Anonymous user