RosettaCodeData/Task/Substring/Haskell/substring-2.hs

32 lines
845 B
Haskell
Raw Permalink Normal View History

2019-09-12 10:33:56 -07:00
{-# LANGUAGE OverloadedStrings #-}
2017-09-23 10:01:46 +02:00
2019-09-12 10:33:56 -07:00
import qualified Data.Text as T (Text, take, drop, init, breakOn)
import qualified Data.Text.IO as O (putStrLn)
fromMforN :: Int -> Int -> T.Text -> T.Text
fromMforN n m s = T.take m (T.drop n s)
2017-09-23 10:01:46 +02:00
fromNtoEnd :: Int -> T.Text -> T.Text
fromNtoEnd = T.drop
allButLast :: T.Text -> T.Text
allButLast = T.init
2019-09-12 10:33:56 -07:00
fromCharForN, fromStringForN :: Int -> T.Text -> T.Text -> T.Text
fromCharForN m needle haystack = T.take m $ snd $ T.breakOn needle haystack
2017-09-23 10:01:46 +02:00
2019-09-12 10:33:56 -07:00
fromStringForN = fromCharForN
2017-09-23 10:01:46 +02:00
2019-09-12 10:33:56 -07:00
-- TEST ---------------------------------------------------
2017-09-23 10:01:46 +02:00
main :: IO ()
main =
mapM_
2019-09-12 10:33:56 -07:00
O.putStrLn
([ fromMforN 9 10
2017-09-23 10:01:46 +02:00
, fromNtoEnd 20
, allButLast
2019-09-12 10:33:56 -07:00
, fromCharForN 6 ""
, fromStringForN 6 "大势"
2017-09-23 10:01:46 +02:00
] <*>
2019-09-12 10:33:56 -07:00
["天地不仁仁者人也🐒话说天下大势分久必合🍑合久必分🔥"])