Haskell IO递归(带二叉树)
我是哈斯凯尔的新手,到目前为止我很喜欢它,但我正在与IO的某些方面作斗争。 我正在制作一种简单的“Akinator”应用程序,我很难理解如何使用IO递归修改二叉树。 我有自己的数据类型:Haskell IO递归(带二叉树),haskell,recursion,io,tree,binary,Haskell,Recursion,Io,Tree,Binary,我是哈斯凯尔的新手,到目前为止我很喜欢它,但我正在与IO的某些方面作斗争。 我正在制作一种简单的“Akinator”应用程序,我很难理解如何使用IO递归修改二叉树。 我有自己的数据类型: data QA = P String | Q QA String QA -- P representing person, Q representing a question deriving (Show, Read) 我们通过回答“是”或“否”来下树。是会把你带到左边,否会把你带到右边。 但是当我在运
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格式),但添加了叶/分支
多谢各位
-------我的一些代码--------
play::QA->IO QA
玩(PS)=做
做
putStrLn“我赢了
exitSuccess--(在不修改
--不知道这是否是一个好的解决方案)
False->do
你会写字
返回(Q-pY-s-qaN)
play(Q qaY s qaN)=do
做
皮杜
pN
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)