
Why not have:
data Question = Question
{ questionName :: Name
, questionText :: QuestionText
, questionType :: QuestionType
, answerFunc :: (String -> AnswerType)
, correctAnswer :: Maybe AnswerType
, options :: Maybe [Option AnswerType]
} deriving (Show)
data AnswerType = AnsD Double | AnsS String | AnsI Integer deriving (Show, Read)
Then, I'd personally make another change, why would you have a flat
structure with a questionType and then optional correctAnswer and
options fields? There's no type safety in that. I'd try:
data Answer = StraightAnswer (String -> AnswerType) | MultipleChoice
AnswerType [Option AnswerType]
data Question = Question
{ questionName :: Name
, questionText :: QuestionText
, answerFunc :: (String -> AnswerType)
, answer :: Answer
} deriving (Show)
If you are storing answers as string, just store them as "AnsD 5.589",
"AnsS \"Constantiople\"". Then with the read instance you can go:
let answer = read x :: AnswerType
On Thu, Nov 24, 2011 at 4:52 PM, Alia
Peter Hall wrote:
Might this be easier if you made all the answers strings? If you need to permit things like 0.5 vs .5 you could instead > use a String->String normalisation function which could vary with the "type" of the answer but not affect the data type > of the question. It would also be easier to use strings if you wanted to store the questions in a database or load from a file.
Indeed that is exactly how I do it. All user entered answers are stored as strings, and are converted to the appropriate type by an answerFunc (see below) and compared against correct answers. The purpose of the answerFunc is simply to convert (String -> type of answer) and can include normalization if required. Fyi, the complete schema is as follows:
data QuestionType = Open | Test | Choice deriving (Show, Eq)
data Question a = Question { questionName :: Name , questionText :: QuestionText , questionType :: QuestionType , answerFunc :: (String -> a) , correctAnswer :: Maybe a , options :: Maybe [Option a] } deriving (Show)
data Question' = QuestionS (Question String) | QuestionI (Question Int) | QuestionD (Question Double) deriving (Show)
data QuestionSet = QuestionSet { qsetTitle :: String , qsetQuestions :: [Question'] , qsetPoints :: Double } deriving (Show)
data Survey = Survey { surveyTitle :: String , surveyQuestionSets :: [QuestionSet] } deriving (Show)
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners