Skip to content

Instantly share code, notes, and snippets.

@maoe
Created September 11, 2010 02:35
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 maoe/574709 to your computer and use it in GitHub Desktop.
Save maoe/574709 to your computer and use it in GitHub Desktop.
unfoldMの使い方
module Main where
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.Loops
import System.Environment
import System.IO
main :: IO ()
main = do
f:_ <- getArgs
wcL f >>= print
wcL :: FilePath -> IO Int
wcL p = bracket (openFile p ReadMode)
hClose
(liftM length . hGetLines)
hGetLines :: Handle -> IO [String]
hGetLines h = unfoldM mb
where mb = do
eof <- hIsEOF h
if eof then return Nothing
else Just <$> hGetLine h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment