File: quiz-lib.rkt

package info (click to toggle)
racket 5.2.1%2Bg6~92c8784%2Bdfsg2-2%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 88,676 kB
  • sloc: ansic: 229,942; sh: 31,883; lisp: 11,486; asm: 9,970; cpp: 2,310; makefile: 2,084; pascal: 2,075; exp: 416; xml: 62; perl: 10; python: 8
file content (87 lines) | stat: -rw-r--r-- 2,294 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#lang racket/base
(require racket/serialize
         web-server/http
         net/url)
(provide (struct-out mc-question)
         make-cue-page
         quiz)

(define-serializable-struct mc-question (cue answers correct-answer))

;; answer-rows: mc-question -> (listof `(tr ...))
;; format the answers as rows for an html table
(define (answer-rows mc-q)
  (let loop ([i 0] [ans-strs (mc-question-answers mc-q)])
    (cond
      [(null? ans-strs) '()]
      [else
       (cons `(tr (td (input ([type "radio"]
                              [name "answs"]
                              [value ,(number->string i)])
                             ,(car ans-strs))))
             (loop (add1 i) (cdr ans-strs)))])))

;; make-cue-page: mc-question -> url hidden-field -> html-page
;; generate the page for the question
(define (make-cue-page mc-q)
  (lambda (ses-url k-hidden)
    (response/xexpr
     `(html (head (title "Question"))
            (body
             (form ([action ,(url->string ses-url)] [method "post"]
                                                    [enctype "application/x-www-form-urlencoded"])
                   ,(mc-question-cue mc-q)
                   (table () ,@(answer-rows mc-q))
                   (input ([type "submit"]))
                   ,k-hidden))))))

(define quiz
  (list
   (make-mc-question
    "How old is the earth?"
    (list
     "6004 years"
     "4.55 billion years"
     "200,000 years")
    1)
   (make-mc-question
    "Do bears like honey?"
    (list "yes" "no")
    0)
   (make-mc-question
    "How tall is the empire state building?"
    (list "1,206 feet"
          "511 meters"
          "1,454 feet"
          "107 floors")
    2)
   (make-mc-question
    "What is the ultimate answer?"
    (list "10" "true" "64" "42")
    3)
   (make-mc-question
    "Where do babies come from?"
    (list
     "The cabbage patch"
     "The stork"
     "A watermelon seed"
     "Wal-Mart")
    1)
   (make-mc-question
    "Who was the first president of the United States?"
    (list
     "Thomas Jefferson"
     "Theodore Roosevelt"
     "George Washington"
     "Douglas Fairbanks"
     "John Adams"
     )
    2)
   (make-mc-question
    "What makes the world go 'round?"
    (list
     "money"
     "love"
     "angular momentum"
     "sex")
    0)))