【发布时间】:2017-07-14 19:19:30
【问题描述】:
我很难准确理解 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)
但这给了我一个错误,因为 getDoubles 返回类型 IO [Double],而不仅仅是 [Double]。 如何将我的 getDoubles 函数映射到我的文件句柄列表上?
【问题讨论】:
-
sendData采用[Double],但您有一个[Double]s 的集合,每个文件一个。您想如何消除这种不匹配?