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

Peter Hall peterjoel at gmail.com
Thu Nov 24 20:24:06 CET 2011


> For example, I can ask a question which requires a Double as an 
answer and another which requires an Int as an answer:

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.  

Peter

On 24 Nov 2011, at 09:32, Alia <alia_khouri at yahoo.com> wrote:

> Hi folks,
> 
> As the code (from my prior post) is not so relevant to the issue
> at hand, it was quite reasonably suggested that I should simplify my
> question somewhat. So here goes:
> 
> In my model, I have a Question record type, which has a type parameter
> to take into account that answers can be evaluated to different types.
> For example, I can ask a question which requires a Double as an 
> answer and another which requires an Int as an answer:
> 
> data Question a = Question {text:: String, answer:: Maybe a}
> 
> This question type is wrapped/boxed in another type to allow the 
> different questions to referenced in a list. Hence,
> 
> data Question' = QuestionS (Question String)
>                | QuestionI (Question Int)
>                | QuestionD (Question Double)
> 
> and I can now store the questions as follows:
> 
> questions = [ QuestionI {text="What's 1+1?", answer=Maybe 2}
>             , QuestionD {text="What's 1.0+1.5?", answer=Maybe 2.5}
>             ]
> 
> Now to extract Question a from Question' I would have liked to write 
> the following:
> 
> extract :: Question' -> Question a
> extract q = case q of
>     QuestionS x -> extractQString q
>     QuestionI x -> extractQInt q
>     QuestionD x -> extractQDouble q
> 
> but instead, I had to produce the following instead:
> 
> extractQString :: Question' -> Question String
> extractQString (QuestionS q) = q
> 
> extractQInt :: Question' -> Question Int
> extractQInt (QuestionI q) = q
> 
> extractQDouble :: Question' -> Question Double
> extractQDouble (QuestionD q) = q
> 
> From what I understand so far, that specialized extraction or unboxing
> functions are the way to go unless one uses language extensions like
> GADTs which can be conceptually more complicated. Is this indeed the case,
> or are there other ways to achieve generalized type unboxing while
> remaining within the Haskell98 specification?
> 
> Best,
> 
> Alia 
>  
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111124/d6c346c9/attachment-0001.htm>


More information about the Beginners mailing list