我是Haskell的新手,我正在寫一個簡單的AI決策系統來玩20個問題的遊戲風格。如果程序無法猜出正確的答案,它會要求提供一個問題來區分答案,並將該問題存儲在樹中。它在程序開始時從文件系統中讀取樹,並在最後將其寫回。調試Haskell閱讀功能
我在Haskell中的序列化代碼有很多問題。我收到錯誤「Prelude read:no parse」。這是怎麼回事?這裏是我的代碼:
import Data.Char
import System.IO
-- Defines a type for a binary tree that holds the questions and answers
data Tree a =
Answer String |
Question String (Tree a) (Tree a)
deriving (Read, Show)
-- Starts the game running
main = do
let filePath = "data.txt"
fileContents <- readFile filePath
animals <- return (read fileContents)
putStrLn "Think of an animal. Hit Enter when you are ready. "
_ <- getLine
ask animals
writeFile filePath (show animals)
-- Walks through the animals tree and ask the question at each node
ask :: Tree a -> IO()
ask (Question q yes no) = do
putStrLn q
answer <- getLine
if answer == "yes" then ask yes
else ask no
ask (Answer a) = do
putStrLn $ "I know! Is your animal a " ++ a ++ "?"
answer <- getLine
if answer == "yes" then computerWins
else playerWins
computerWins = do putStrLn "See? Humans should work, computers should think!"
playerWins = do putStrLn "TODO"
而且這裏是我使用的數據文件:
Question "Does it live in the water?"
((Question "Does it hop?") (Answer "frog") (Answer "fish"))
(Answer "cow")
'Tree'不需要類型限定符 - 您可以輕鬆地用'Tree'替換所有'Tree a',而無需更改程序行爲。 – 2010-11-16 18:29:20