-- Example Action Diagrams -- ** -- Diagrams contain Questions, Actions, and "Done" data Diagram = Question String Diagram Diagram | Action String Diagram | Done deriving Show program = Action "Do some programming!" Done park = Action "Go to the park!" Done isSunny = Question "Is it sunny?" park program -- Examples play :: Diagram -> IO() play Done = putStrLn "Done" play (Action s d) = do putStrLn s play d play (Question s y n) = do putStrLn s r <- getLine case r of ('y':_) -> play y ('n':_) -> play n _ -> play (Question s y n) pictionary :: Diagram pictionary = Action "Draw a picture" q where q = (Question "Did they guess it?" (Action "You win" Done) (Action "Point at picture" q) ) -- -- one diagram followed by another? -- (Do the first one, then the second) -- Diagrams with loops? -- Defining diagrams with more open-ended questions data GDiagram = GDone | GAction String GDiagram | GQuestion String [(String,GDiagram)] exampleGD = GQuestion "Hur må du?" [("bra", a),("ok", b),("inte bra", c)] where a = GAction "Toppen!" GDone b = GAction "OK" GDone c = GAction "Synd!" GDone -- Harder exercise: -- Printing diagrams on the screen (text) whyWorry = Action "Then why worry?" Done problems = Question "Do you have problems?" (Question "Can you do anything about them?" whyWorry whyWorry ) whyWorry {- > putStrLn $ showDiagram problems Do you have problems? | + Yes:Can you do anything about them? | | | + Yes:Then why worry? | | | | - No :Then why worry? | | - No :Then why worry? | -}