我在Haskell中进行递归思考时遇到了问题。
我正在尝试构建一个调查应用程序,其中问题根据用户的答案有条件地引发新问题。
我:
- Questions-问题列表
- QuestionPaths-的这会导致新问题的问题,问题的路径列表
- Answers用户的回答列表
您可以将其QuestionPaths视为元组列表,其中:
type QuestionPath = (QuestionId, AnswerChoice, NextQuestionId)
Run Code Online (Sandbox Code Playgroud)
基本上,这将是:如果用户回答一个问题 QuestionId 一个答案 AnswerChoice,问他 NextQuestionId 旁边。
我试图用多路树(节点可以有多个孩子)来为这个问题域建模:
data YesNo = Yes | No
data AnswerChoice = Skip
| Boolean YesNo
| ChooseOne [Text]
type Condition = AnswerChoice
data QuestionTree = QuestionNode {
question :: Question
, condition :: Condition
, userAnswer :: Maybe AnswerChoice
, children :: QuestionForest
}
type QuestionForest = [QuestionTree]
Run Code Online (Sandbox Code Playgroud)
不幸的是,我现在对如何编写像这样组成树的算法一无所知。 …