Monday, November 02, 2009

Haskell...

RealWorldHaskell을 조금씩 읽어 나가던 중, 일본판이 나왔다는 소식에 엔화가 떨어지기를 기다리는 중. 아마 그때 쯤이면 Graham Hutton의 일어판도 같이 나오지 않을 까.
암튼...
그때는 꼭 ヨコハマ買い出し紀行(카페알파)도 구입할 수 있게 되기를...

최근의 수확은 다음의 것.

File Reading in Haskell

module Main
where
import IO

main = do hSetBuffering stdin LineBuffering
doLoop

doLoop = do
putStrLn "Enter a command rFN WFN or q to quit: "
command <- getLine
case command of
'q':_ -> return ()
'r':filename -> do putStrLn ("Reading " ++ filename)
doRead filename
doLoop
'w':filename -> do putStrLn ("Writing " ++ filename)
doWrite filename
doLoop
_ -> doLoop
doRead filename =
bracket (openFile filename ReadMode) hClose
(\h -> do contents <- hGetContents h
--putStrLn "The first 100 chars: "
putStrLn (head $ lines contents)) -- hgetContents는 IO String을 반환. ->를 통해서 Var에 String으로 저장.
--putStrLn (take 100 contents))
doWrite filename = do
putStrLn "Enter text to go into the file: "
contents <- getLine
bracket (openFile filename WriteMode) hClose
(\h -> hPutStrLn h contents)

No comments: