File: Multiplication.hs

package info (click to toggle)
washngo 2.9-4.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,876 kB
  • ctags: 273
  • sloc: haskell: 54,162; makefile: 1,086; ansic: 305; sh: 153; sql: 13
file content (65 lines) | stat: -rw-r--r-- 2,070 bytes parent folder | download
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
--  2001, 2002 Peter Thiemann
module Main where

import Prelude hiding (head, div, span, map)
import WASH.HTML.HTMLMonad
import Random
import WASH.CGI.CGI

main = 
  run mdrill

question prompt def =
  tr (do td (text prompt)
	 td (inputField (fieldVALUE def)))

mdrill =
  (standardQuery "Multiplication Drill" $ table $
       do factor <- question "Choose a factor: " 2
	  rept   <- question "Number of exercises: " 20
	  tr (td (submit (F2 factor rept) multiply (fieldVALUE "START"))))

multiply (F2 factor rept) = 
  let { n, r :: Int; 
	n = value factor; 
	r = value rept } in
  multiplyBy n r 1 [] []

multiplyBy n r i successes failures =
  if i == r + 1
  then report successes failures
  else io (randomRIO (0,12)) >>= \factor ->
  let ttl = "Multiplication Drill; Exercise " ++ show i ++ " of " ++ show r in
  (standardQuery ttl 
      (do text (show factor ++ " x " ++ show n ++ " = ")
	  activeInputField (checkAnswer n r i factor successes failures)
	                   (fieldSIZE 3)))

checkAnswer n r i factor successes failures a =
  let correct = a == n * factor 
      ttl = "Multiplication Drill; Solution " ++ show i ++ " of " ++ show r
  in
  standardQuery ttl $
    do text (show factor ++ " x " ++ show n ++ " = " ++ show (n * factor))
       text (". Your answer: " ++ show a ++ ". ")
       if correct then text "CORRECT!" else text "WRONG."
       br empty
       submit0 (multiplyBy n r (i + 1) 
	       (if correct then factor : successes else successes)
	       (if correct then failures else factor : failures))
	       (fieldVALUE "CONTINUE")

report successes failures =
  standardQuery "Final Report" (evaluation successes failures)

evaluation successes failures =
  let s = length successes
      f = length failures
      n = s + f
      comment
       | f == 0 = text "Excellent!"
       | s >= 2 * f = text ("Good, but you need to look at " ++ show failures)
       | s >= f = text ("Fair, but you must look at " ++ show failures)
       | s <  f = text ("Unsatisfactory. You need to practice " ++ show failures)
  in  comment