Skip to content

Instantly share code, notes, and snippets.

@veer66
Created September 4, 2016 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veer66/a35cbb4a521ea02cf992e27fb1a115b4 to your computer and use it in GitHub Desktop.
Save veer66/a35cbb4a521ea02cf992e27fb1a115b4 to your computer and use it in GitHub Desktop.
By using ByteString, this program becomes much faster than text/string version.
import qualified Data.ByteString.Char8 as B
-- https://hackage.haskell.org/package/bytestring-0.10.8.1/docs/Data-ByteString-Char8.html
splitBS x y = h : if B.null t then [] else splitBS x (B.drop (B.length x) t) where (h,t) = B.breakSubstring x y
countSplit row = length $ splitBS (B.pack " ||| ") row
readLines = B.getContents >>= (return . B.lines)
countToks lines = foldl (+) 0 (map countSplit lines)
main = readLines >>= (return . countToks) >>= (putStrLn . show)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment