Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  data design for a questionnaire (retitled +  update) (Alia)
   2.  help opengl object (Song Zhang)
   3. Re:  data design for a questionnaire (retitled +  update) (Alia)


----------------------------------------------------------------------

Message: 1
Date: Wed, 23 Nov 2011 03:07:20 -0800 (PST)
From: Alia <[email protected]>
Subject: Re: [Haskell-beginners] data design for a questionnaire
        (retitled +     update)
To: "[email protected]" <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi folks,

As a follow-on question from my prior post, I got stuck on not being able to 
compile the following
code in my survey data model which basically generally unwraps the the inner 
type from the 
wrapper:

extract :: Question' -> Question a
extract q = case q of
??? QuestionS x -> extractQString q
??? QuestionI x -> extractQInt q
??? QuestionD x -> extractQDouble q

and 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

An exchange in stackoverflow seems to suggest that the only way to do it is to 
use GADTs (which is a language extension).
http://stackoverflow.com/questions/6047522/haskell-type-and-pattern-matching-question-extracting-fields-from-a-data-type

I'm disinclined to use language extensions because I'd rather use haskell98 
proper to begin with. So Is this indeed the case?
?My code has become more verbose as a result and I am trying to learn most 
elegant and general haskell 'way' (if it exists) 
and hence I would appreciate any advice to this end.

regards,

Alia


<survey.hs>

module Main where

import Text.Show.Functions
import Data.Maybe

type Name??????????? = String
type QuestionText??? = String
type Answer????????? = String
type Score?????????? = Double
type CorrectAnswer a = a
type Option a??????? = (String, a)

-- type converters
str????? = id
int s??? = read s :: Int
double s = read s :: Double

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)



parse :: Question a -> Answer -> a
parse = answerFunc

view? :: Question a -> String
view q = questionName q

ask?? :: Question a -> IO ()
ask q = putStrLn $ questionText q

store :: Question a -> Answer -> IO ()
store q ans = putStrLn $ questionName q ++ ": " ++ show ans

{-
extract :: Question' -> Question a
extract q = case q of
??? QuestionS x -> extractQString q
??? QuestionI x -> extractQInt q
??? QuestionD x -> extractQDouble q
-}

extractQString :: Question' -> Question String
extractQString (QuestionS q) = q

extractQInt :: Question' -> Question Int
extractQInt (QuestionI q) = q

extractQDouble :: Question' -> Question Double
extractQDouble (QuestionD q) = q

testQ :: (Eq a) => Question a -> Answer -> Bool
testQ q ans = case (correctAnswer q) of
??? Nothing -> False
??? Just x? -> x == (answerFunc q $ ans)

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


testQset :: QuestionSet -> [Answer] -> [Bool]
testQset qs as = zipWith testQ' (qsetQuestions qs) as


evalQset :: QuestionSet -> [Answer] -> Score
evalQset qs as = (total_correct / total_questions) * score
??? where
??????? total_questions = fromIntegral (length $ qsetQuestions qset)
??????? total_correct = fromIntegral (length $ filter (== True) (testQset qset 
as))
??????? score = qsetPoints qset


q1 = Question
??? { questionName? = "q1"
??? , questionText? = "What is our name?"
??? , questionType? = Open
??? , answerFunc??? = id
??? , correctAnswer = Nothing
??? , options?????? = Nothing
??? }

q2 = Question
??? { questionName? = "q2"
??? , questionText? = "What is 1+1?"
??? , questionType? = Test
??? , answerFunc??? = int
??? , correctAnswer = Just 2
??? , options?????? = Nothing
??? }

q3 = Question
??? { questionName? = "q3"
??? , questionText? = "What is 2+1?"
??? , questionType? = Choice
??? , answerFunc??? = int
??? , correctAnswer = Just 3
??? , options?????? = Just [("a", 2), ("b", 3), ("c", 4)]
??? }

q4 = Question
??? { questionName? = "q4"
??? , questionText? = "What is 2.0 + 1.5 ?"
??? , questionType? = Choice
??? , answerFunc??? = double
??? , correctAnswer = Just 3.5
??? , options?????? = Just [("a", 2.1), ("b", 3.5), ("c", 4.4)]
??? }


qset = QuestionSet
??? { qsetTitle???? = "simple questions"
??? , qsetQuestions = [ QuestionS q1 
????????????????????? , QuestionI q2 
????????????????????? , QuestionI q3
????????????????????? , QuestionD q4
????????????????????? ]
??? , qsetPoints??? = 100.0
??? }

survey = Survey
??? { surveyTitle??????? = "a survey"
??? , surveyQuestionSets = [qset]
??? }
??? 
t1 = evalQset qset ["1", "2", "3", "4"]

</survey.hs>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111123/e78cb9e8/attachment-0001.htm>

------------------------------

Message: 2
Date: Wed, 23 Nov 2011 22:58:26 +0000
From: Song Zhang <[email protected]>
Subject: [Haskell-beginners] help opengl object
To: [email protected]
Message-ID:
        <CACGMEOnX-5HOOjUPY2htsCx5C2nEFGvy=zdddq5gl0ooow+...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi
     in Graphics.UI.GLUT.Objects
     there are some pre-defined object such as cube or cone
     I tried to draw objects in haskell but get black screen
     who can give me some examples to draw these objects
     and for drawing Cylinder  in the documentation what is (*freeglut
only) *means. do I need glut32.dll to render these object in windows?
     thanks

-- 
the University of Nottingham, CS school
the representative of SSFC CS school
Staff of SIFEUNNC IT department
I am Ice which is sleeping water.
Sent via BlackBerry? by BerryMail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111123/8fff9573/attachment-0001.htm>

------------------------------

Message: 3
Date: Thu, 24 Nov 2011 01:32:47 -0800 (PST)
From: Alia <[email protected]>
Subject: Re: [Haskell-beginners] data design for a questionnaire
        (retitled +     update)
To: "[email protected]" <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

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

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 41, Issue 32
*****************************************

Reply via email to