[Haskell-beginners] questionnaire data design patterns
Amy de Buitléir
amy at nualeargais.ie
Tue Nov 22 19:09:50 CET 2011
Hi Alia,
Here's my suggestion. It does compile.
Of course, there are lots of ways to approach this kind of thing. My choice was
to refactor it so that the Answer type contains the correct answer and the
possible answers, as appropriate. Then when you write the method that prompts
the user for an answer, and the method that checks the user's answer, you can
pattern match on the Answer type.
-----
module Main where
data Answer = Open
| Test { correctIntAnswer :: Int }
| Choice { correctStringAnswer :: Int, options :: [(String, String)] }
deriving (Show, Eq)
data Question = Question
{ questionName :: String
, questionText :: String
, answer :: Answer
} deriving (Show, Eq)
data QuestionSet = QuestionSet
{ qsetTitle :: String
, qsetQuestions :: [Question]
} deriving (Show, Eq)
data Questionnaire = Questionnaire
{ questionnaireTitle :: String
, questionnaireQuestionSets :: [QuestionSet]
} deriving (Show, Eq)
q1 = Question
{ questionName = "q1"
, questionText = "What is our name?"
, answer = Open
}
q2 = Question
{ questionName = "q2"
, questionText = "What is 1+1?"
, answer = Test 2
}
q3 = Question
{ questionName = "q2"
, questionText = "What is 2+1?"
, answer = Choice 3 [("1", "2"), ("2", "3"), ("3", "4")]
}
qset = QuestionSet
{ qsetTitle = "simple questions"
, qsetQuestions = [q1, q2, q3]
}
questionnaire = Questionnaire
{ questionnaireTitle = "a questionnaire"
, questionnaireQuestionSets = [qset]
}
More information about the Beginners
mailing list