Workshop 07 A

Embed Size (px)

Citation preview

  • 7/30/2019 Workshop 07 A

    1/2

    Declarative Programming

    Answers to workshop exercises set 7a.

    QUESTION 1

    Write a Haskell implementation of the old Animals guessing game.Start by prompting "Think of an animal. Hit return when ready." Waitfor the user to hit return, then ask: "Is it a penguin?" and wait fora Y or N answer. If the answer is yes, print out that you guessed itwith 0 questions. If no, then ask them what their animal was, askthem to enter a yes or no question that would distinguish their animalfrom a penguin, and whether the answer is yes or no for their animal.

    Then start over. This time start by asking them the question theyjust entered, and depending on their answer, and the answer they saidto expect for their previous animal, ask them if their (new) animal istheir previous animal, or ask if it is a penguin. If that is correct,print out that you guessed it with 1 question. If no, then ask themwhat their animal was, ask them to enter a yes or no question thatwould distinguish their animal from the one you just guessed, andwhether the answer is yes or no for their animal.

    The game proceeds like this indefinitely. You should build up a

    decision tree with questions at the nodes, and animals at the leaves.For each animal they think of, you traverse the tree from the rootasking questions and following the branch selected by their answeruntil you reach a leaf, then guess the animal at the leaf, and getthem to give you a question to extend the tree if you get it wrong.

    ANSWER

    > import Data.Char

    > main :: IO ()> main = do> mainloop $ Leaf "penguin"

    > bye

    > mainloop :: DTree -> IO ()> mainloop dtree = do> putStrLn ""> putStrLn "Think of an animal."> putStr "Hit return when you are ready. "> getLine> dtree' again if again then mainloop dtree' else return ()

    > bye :: IO ()

    > bye = putStrLn "Thanks for playing!"

    > yesOrNo :: String -> IO Bool> yesOrNo prompt = do> response case dropWhile isSpace response of> [] -> yesOrNoAgain prompt> (char:_) -> case toLower char of> 'y' -> return True> 'n' -> return False

  • 7/30/2019 Workshop 07 A

    2/2

    > (_) -> yesOrNoAgain prompt

    > yesOrNoAgain :: String -> IO Bool> yesOrNoAgain prompt = do> putStrLn "Please answer yes or no."> yesOrNo prompt

    > promptedRead :: String -> IO String> promptedRead prompt = do> putStr prompt> response let response' = dropWhile isSpace response> if response == [] then do> putStrLn "Please answer the question."> promptedRead prompt> else return response'

    > data DTree = Choice String DTree DTree> | Leaf String

    > animalRound :: DTree -> Int -> IO DTree> animalRound (Leaf animal) depth = do> answer if answer then do

    > putStrLn $ "I guessed it with " ++ show depth ++ " questions."> return $ Leaf animal> else do> animal' putStrLn $ "Please type a question that would distinguish a "> ++ animal' ++ " from a " ++ animal ++ "."> question' answer' "What is the answer to this question for a "> ++ animal' ++ "?"> return $ Choice question'> (Leaf $ if answer' then animal' else animal)> (Leaf $ if answer' then animal else animal')

    > animalRound (Choice question yesTree noTree) depth = do> answer dtree return $ Choice question> (if answer then dtree else yesTree)> (if answer then noTree else dtree)