在Haskell中,我可以轻松地定义一个递归函数,该函数接受一个值并返回一个字符串:
出现以下错误:
Prelude> let countdown i = if (i > 0) then (show i) ++ countdown (i-1) else ""
Prelude> countdown 5
"54321"
我希望使用相同的设计从文件句柄中读取可用数据。在这种特殊情况下,我需要以与hGetContents相同的方式读取数据,但不会使句柄处于“半关闭”状态,以便我可以循环与使用createProcess打开的stdin / stdout处理交互:
main = do
-- do work to get hin / hout handles for subprocess input / output
hPutStrLn hin "whats up?"
-- works
-- putStrLn =<< hGetContents hout
putStrLn =<< hGetLines hout
where
hGetLines h = do
readable <- hIsReadable h
if readable
then hGetLine h ++ hGetLines h
else []
出现以下错误:
Couldn't match expected type `IO b0' with actual type `[a0]'
In the expression: hGetLine h : hGetLines h
我知道有各种库可用于完成我想要完成的任务,但由于我正在学习,所以我的问题实际上是如何执行递归IO。谢谢!
hGetLines
函数? - Don Stewart