2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,16 +1,5 @@
|
|||
splitBy :: (a -> Bool) -> [a] -> [[a]]
|
||||
splitBy _ [] = []
|
||||
splitBy f list = first : splitBy f (dropWhile f rest) where
|
||||
(first, rest) = break f list
|
||||
{-# OPTIONS_GHC -XOverloadedStrings #-}
|
||||
import Data.Text (splitOn,intercalate)
|
||||
import qualified Data.Text.IO as T (putStrLn)
|
||||
|
||||
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"
|
||||
main = T.putStrLn . intercalate "." $ splitOn "," "Hello,How,Are,You,Today"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,16 @@
|
|||
*Main> mapM_ putStrLn $ takeWhile (not.null) $ unfoldr (Just . second(drop 1). break (==',')) "Hello,How,Are,You,Today"
|
||||
Hello
|
||||
How
|
||||
Are
|
||||
You
|
||||
Today
|
||||
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-3.hs
Normal file
6
Task/Tokenize-a-string/Haskell/tokenize-a-string-3.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