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

Alia alia_khouri at yahoo.com
Thu Nov 24 22:52:54 CET 2011


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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111124/1baed91a/attachment.htm>


More information about the Beginners mailing list