Tokenize a string: Difference between revisions
Content deleted Content added
Added two solutions for Bracmat |
Undo revision 115192 by 86.145.79.157 (talk) revert Haskell entry; new version uses non-standard library and deleted many examples |
||
Line 548: | Line 548: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
The necessary operations are unfortunately not in the standard library (yet), but simple to write: |
|||
<lang haskell> |
|||
⚫ | |||
import Data.List.Split |
|||
<lang haskell>splitBy :: (a -> Bool) -> [a] -> [[a]] |
|||
⚫ | |||
splitBy _ [] = [] |
|||
⚫ | |||
splitBy f list = first : splitBy f (dropWhile f rest) where |
|||
(first, rest) = break f list |
|||
splitRegex :: Regex -> String -> [String] |
|||
joinWith :: [a] -> [[a]] -> [a] |
|||
joinWith d xs = concat $ List.intersperse d xs |
|||
-- "concat $ intersperse" can be replaced with "intercalate" from the Data.List in GHC 6.8 and later |
|||
⚫ | |||
-- using regular expression to split: |
|||
⚫ | |||
putStrLn $ joinWith "." $ splitRegex (mkRegex ",") $ "Hello,How,Are,You,Today"</lang> |
|||
Tokenizing can also be realized by using unfoldr and break: |
|||
<lang Haskell>*Main> mapM_ putStrLn $ takeWhile (not.null) $ unfoldr (Just . second(drop 1). break (==',')) "Hello,How,Are,You,Today" |
|||
Hello |
|||
How |
|||
Are |
|||
You |
|||
⚫ | |||
* You need to import the modules Data.List and Control.Arrow |
|||
As special cases, splitting / joining by white space and by newlines are provided by the Prelude functions <tt>words</tt> / <tt>unwords</tt> and <tt>lines</tt> / <tt>unlines</tt>, respectively. |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |