And*_*Pos 3 haskell functional-programming
我正在学习Haskell,我需要比较两个文件.我没有找到这样做的函数,所以我自己编写了它.以下是我提出的功能.
cmpFiles :: FilePath -> FilePath -> IO Bool
cmpFiles a b = withBinaryFile a ReadMode $ \ha ->
withBinaryFile b ReadMode $ \hb ->
fix (\loop -> do
isEofA <- hIsEOF ha
isEofB <- hIsEOF hb
if | isEofA && isEofB -> return True -- both files reached EOF
| isEofA || isEofB -> return False -- only one reached EOF
| otherwise -> do -- read content
x <- hGet ha 4028 -- TODO: How to use a constant?
y <- hGet hb 4028 -- TODO: How to use a constant?
if x /= y
then return False -- different content
else loop -- same content, contunue...
)
Run Code Online (Sandbox Code Playgroud)
我的问题是:
怎么样
cmpFiles a b = do
aContents <- readFile a
bContents <- readFile b
return (aContents == bContents)
Run Code Online (Sandbox Code Playgroud)