【发布时间】:2020-10-20 04:07:30
【问题描述】:
我是 Haskell 的新手,到目前为止我很喜欢它,但我在 IO 的某些方面遇到了困难。 我正在制作一种简单的“Akinator”应用程序,但我无法理解如何使用 IO 递归修改二叉树。 我有自己的数据类型:
data QA = P String | Q QA String QA -- P representing person, Q representing a question
deriving (Show, Read)
我们通过回答“是”或“否”来回答问题。是带你到左边,不带你到右边。 但是当我在运行程序时到达树的末尾时(我们到达一个人),我希望能够通过添加另一个 QA 来修改树。 (如果找不到人,我们会要求用户输入)
我使用的功能是:
play :: QA -> IO QA
如何在递归过程中将 QA 数据类型转换为 IO QA 并返回相同的 QA(以 IO 格式)但添加了 Leaf/Branch?
非常感谢!
-------我的一些代码--------
play :: QA -> IO QA
play (P s) = do
a <- yesNoQuestion ("Is ( " ++s ++" ) your person?")
case a of
True -> do
putStrLn "I win omg so ez!"
exitSuccess -- (to exit the application without modifying
-- dunno if this is a good solution)
False -> do
p <- question "Just curious: Who was your famous person?"
n <- question "Give me a question for which the answer for this person is yes"
return q -- (Here I want to return a new QA with an added question --
--and with a leaf representing a person)
play (Q qaY s qaN) = do
a <- yesNoQuestion s
case a of
True -> do
pY <- play qaY
return pY -- (I want this to say (return Q pY s qaN) But this
-- does not work
-- since all types aren't IO)
False -> do
pN <- play qaN
return pN -- (Same as previous)
【问题讨论】:
标签: haskell recursion io tree binary