all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
16
Task/Tokenize-a-string/Haskell/tokenize-a-string-1.hs
Normal file
16
Task/Tokenize-a-string/Haskell/tokenize-a-string-1.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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
|
||||
|
||||
putStrLn $ joinWith "." $ splitBy (== ',') $ "Hello,How,Are,You,Today"
|
||||
|
||||
-- using regular expression to split:
|
||||
import Text.Regex
|
||||
putStrLn $ joinWith "." $ splitRegex (mkRegex ",") $ "Hello,How,Are,You,Today"
|
||||
6
Task/Tokenize-a-string/Haskell/tokenize-a-string-2.hs
Normal file
6
Task/Tokenize-a-string/Haskell/tokenize-a-string-2.hs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
*Main> mapM_ putStrLn $ takeWhile (not.null) $ unfoldr (Just . second(drop 1). break (==',')) "Hello,How,Are,You,Today"
|
||||
Hello
|
||||
How
|
||||
Are
|
||||
You
|
||||
Today
|
||||
Loading…
Add table
Add a link
Reference in a new issue