[Haskell-beginners] data design for a questionnaire (retitled + update)
Alia
alia_khouri at yahoo.com
Thu Nov 24 22:44:27 CET 2011
> extract :: Question' -> Question a
> extract q = case q of
> QuestionS x -> extractQString q
> QuestionI x -> extractQInt q
> QuestionD x -> extractQDouble q
Antoine wrote:
>> What is a caller supposed to do with this function? How were you
>> imagining it would be called?
The sole purpose of the above function would be to unbox the inner parametrized (Question a) type
from its (Question') wrapper. If it were possible I could write this:
testQ' :: Question' -> Answer -> Bool
testQ' q a = testQ (extract q) a
given:
testQ :: (Eq a) => Question a -> Answer -> Bool
testQ q ans = case (correctAnswer q) of
Nothing -> False
Just x -> x == (answerFunc q $ ans)
instead of this
testQ' :: Question' -> Answer -> Bool
testQ' q a = case q of
QuestionS x -> testQS q a
QuestionI x -> testQI q a
QuestionD x -> testQD q a
where
testQS q a = testQ (extractQString q) a
testQI q a = testQ (extractQInt q) a
testQD q a = testQ (extractQDouble q) a
In fact this seemingly redundant pattern emerges in all wrapper handling code, for instance:
askQ' :: Question' -> IO Answer
askQ' q = case q of
QuestionS x -> askQS q
QuestionI x -> askQI q
QuestionD x -> askQD q
where
askQS q = askQ (extractQString q)
askQI q = askQ (extractQInt q)
askQD q = askQ (extractQDouble q)
Hope this clarifies things somewhat.
Alia
Alia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111124/05df6f05/attachment.htm>
More information about the Beginners
mailing list