対話機能を使用しようとしていますが、次のコードに問題があります。
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 であるため、interact の有効な入力である必要があるようです。
編集/更新
私は以下を試しましたが、うまくいきます。unlines と lines を使用すると、interact はどのように期待どおりに動作しますか?
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