【发布时间】:2013-01-27 07:05:16
【问题描述】:
我正在尝试使用交互功能,但遇到以下代码问题:
main::IO()
main = interact test
test :: String -> String
test [] = show 0
test a = show 3
我正在使用 EclipseFP 并接受一个输入,似乎有一个错误。尝试再次运行 main 会导致:
*** Exception: <stdin>: hGetContents: illegal operation (handle is closed)
我不确定为什么这不起作用,测试类型是 String -> String 并且 show 是 Show a => a -> String,所以看起来它应该是交互的有效输入。
编辑/更新
我尝试了以下方法,效果很好。 unlines 和 lines 的使用如何导致交互按预期工作?
main::IO()
main = interact respondPalindromes
respondPalindromes :: String -> String
respondPalindromes =
unlines .
map (\xs -> if isPal xs then "palindrome" else "not a palindrome") .
lines
isPal :: String -> Bool
isPal xs = xs == reverse xs
【问题讨论】:
-
这是一个已知的 GHCI 恼人的特性。见here。
-
@n.m.感谢您的链接,但是该链接中的任何“解决方案”,
:load、:reload或:set +r都不起作用。有任何想法吗?我不想在每次getContents >>= print类型操作后重新启动 ghci。
标签: haskell eclipse-fp