Skip to content

Instantly share code, notes, and snippets.

@codygman
Created December 16, 2013 09:23
Show Gist options
  • Save codygman/7984368 to your computer and use it in GitHub Desktop.
Save codygman/7984368 to your computer and use it in GitHub Desktop.
How to get the length of a webpage in haskell imperatively, using Network.HTTP, and using Network.HTTP.Conduit
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy.Char8 as BCL
import qualified Network.HTTP.Conduit as C
import Network.HTTP
pageCountShort :: IO ()
-- prints word count of a page using Network.HTTP.Conduit
pageCountShort = getLine >>= C.simpleHttp >>= print . length . words . BCL.unpack
main :: IO ()
main = do
-- pageCountVerbose
pageCountShort
-- pageCountShortAlt
-- other ways of doing this
pageCountVerbose :: IO ()
-- imperative and verbose first try getting number of words on a webpage
pageCountVerbose = do
putStrLn "Enter a url: "
url <- getLine
pageText <- C.simpleHttp url
let pageTextCount = length . words . BCL.unpack $ pageText
print $ "There are " ++ show pageTextCount ++ " words!"
pageCountShortAlt :: IO ()
-- print word count of a page using Network.HTTP
pageCountShortAlt = getLine >>= simpleHTTP . getRequest >>= getResponseBody >>= print . length . words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment