Note that there are some explanatory texts on larger screens.

plurals
  1. PODebugging Haskell read function
    primarykey
    data
    text
    <p>I'm new to Haskell and I'm writing a simple AI decision system to play a 20 questions style of game. If the program is unable to guess the correct answer, it will ask for a question to use to distinguish the answer and it will store that question in a tree. It reads the tree in from the file system at the start of the program and writes it back out at the end.</p> <p>I'm having a lot of problems with the serialization code in Haskell. I'm getting the error "Prelude read: no parse". What's going on? Here's my code:</p> <pre><code>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 &lt;- readFile filePath animals &lt;- return (read fileContents) putStrLn "Think of an animal. Hit Enter when you are ready. " _ &lt;- getLine ask animals writeFile filePath (show animals) -- Walks through the animals tree and ask the question at each node ask :: Tree a -&gt; IO () ask (Question q yes no) = do putStrLn q answer &lt;- getLine if answer == "yes" then ask yes else ask no ask (Answer a) = do putStrLn $ "I know! Is your animal a " ++ a ++ "?" answer &lt;- getLine if answer == "yes" then computerWins else playerWins computerWins = do putStrLn "See? Humans should work, computers should think!" playerWins = do putStrLn "TODO" </code></pre> <p>And here's the data file I'm using:</p> <pre><code>Question "Does it live in the water?" ((Question "Does it hop?") (Answer "frog") (Answer "fish")) (Answer "cow") </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload