我正在尝试使用交互功能,但我遇到以下代码的问题:
main::IO()
main = interact test
test :: String -> String
test [] = show 0
test a = show 3
Run Code Online (Sandbox Code Playgroud)
我正在使用EclipseFP并且输入一个输入似乎有错误.尝试再次运行main会导致:
*** Exception: <stdin>: hGetContents: illegal operation (handle is closed)
Run Code Online (Sandbox Code Playgroud)
我不确定为什么这不起作用,测试类型是String - > String并且show是Show a => a - > String,所以看起来它应该是一个有效的交互输入.
编辑/ UPDATE
我尝试了以下,它工作正常.如何使用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 = …Run Code Online (Sandbox Code Playgroud)