Haskell 哈斯凯尔·伊奥的麻烦

Haskell 哈斯凯尔·伊奥的麻烦,haskell,io,Haskell,Io,我很难准确理解haskell是如何处理输入和输出的。作为一个例子,这里我尝试获取数据文件列表的命令行参数,并将这些文件的内容放入双精度列表中。经过一些研究,我的最佳实施方案是: {- Read Doubles from input, sent them to a list -} getDoubles :: Handle -> IO [Double] getDoubles handle = do done <- hIsEOF handle if done

我很难准确理解haskell是如何处理输入和输出的。作为一个例子,这里我尝试获取数据文件列表的命令行参数,并将这些文件的内容放入双精度列表中。经过一些研究,我的最佳实施方案是:

{- Read Doubles from input, sent them to a list -}
getDoubles :: Handle -> IO [Double]
getDoubles handle = do
        done <- hIsEOF handle
        if done then return []
        else do
                first <- hGetLine handle
                rest <- getDoubles handle
                return (read first : rest)

{- Send the data to the handle -}
sendData :: Handle -> [Double] -> IO()
sendData handle (x:[]) = hPrint handle x
sendData handle (x:xs) = do
        hPrint handle x
        sendData handle xs


main = do
        args <- getArgs 
        handleList <- mapM (\x -> openFile x ReadMode) args
        sendData stdout  (fmap getDoubles handleList)
{Read double from input,发送到列表-}
getDoubles::Handle->IO[Double]
getDoubles句柄=do

完成就像您使用
openFile
(它也执行IO)一样,您可以使用
mapM

main = do
    args <- getArgs
    handleList <- mapM (\x -> openFile x ReadMode) args
    doubless <- mapM getDoubles handleList
    sendData stdout doubless

就像您使用
openFile
(它也执行IO)一样,您可以使用
mapM

main = do
    args <- getArgs
    handleList <- mapM (\x -> openFile x ReadMode) args
    doubless <- mapM getDoubles handleList
    sendData stdout doubless

sendData
接受一个
[Double]
,但是您有一个
[Double]
的集合,每个文件一个。如何消除这种不匹配?
sendData
需要一个
[Double]
,但是您有一个
[Double]
的集合,每个文件一个。你想如何消除这种不匹配?(这个答案掩盖了
sendData::…->[Double]->…
doubless:[[Double]]]
之间的不匹配。如果john回答了我对他们问题的评论,我会解决这个问题。)(这个答案掩盖了
sendData:…->[Double]之间的不匹配。)->…
doubless::[[Double]]
。如果john回答了我对他们问题的评论,我会解决的。)