[Haskell-beginners] data design for a questionnaire (retitled + update)

Alia alia_khouri at yahoo.com
Sat Nov 26 15:10:03 CET 2011


 David McBride wrote:
> 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

Thank you very much for the reply which is eye-opening. But I do have to spend time 
implementing your revised schema in the prior question handling functions to 
get my head around it  (-:

Best,

Alia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111126/9bd9e377/attachment.htm>


More information about the Beginners mailing list