Week 3: student numbers; tarball
[fp1415.git] / fp2 / week3 / mart / GetallenRaden.icl
1 // Mart Lubbers s4109503, Camil Staps s4498062
2
3 module GetallenRaden
4
5 import StdEnv
6 import StdDynamic, StdDynamicFileIO
7 import StdFileSelect
8
9 // Example generation program
10 //fib :: [Int]
11 //fib = [fib` i \\ i <- [0..]]
12 // where
13 // fib` 0 = 0
14 // fib` 1 = 1
15 // fib` n = fib` (n-1) + fib` (n-2)
16 //
17 //Start :: *World -> (Bool, *World)
18 //Start world = writeDynamic "fib" (dynamic fib) world
19
20 makeSeq :: Dynamic -> [Int]
21 makeSeq (x :: [Int]) = x
22 makeSeq _ = abort "You selected a file not containing a sequence"
23
24 basename :: String Int -> String
25 basename s 0 = toString s.[0]
26 basename s n = let c = toString s.[n] in if (c == "\\") "" ((basename s (n-1)) +++ c)
27
28 loadSeq :: String *World -> *([Int], String, *World)
29 loadSeq s world
30 # s = let l = size s in s % (0, l-5)
31 # (ok, dyn, world) = readDynamic s world
32 | not ok = abort "You didn't select a dynamic file"
33 | otherwise = (makeSeq dyn, basename s ((size s)-1), world)
34
35 play :: *File String [Int] Int Int -> *File
36 play io _ _ _ 5 = io <<< "Congratulations, you had 5 correct answers\n"
37 play io seqname [nextnum:sequence] currentnum correct
38 # io = io <<< (seqname +++ "[" +++ toString currentnum +++ "] = " +++ toString nextnum +++ "\n")
39 # (ok, guessednumber, io) = freadi io
40 | not ok = play (snd (freadline io)) seqname sequence (currentnum + 1) correct
41 # (io, correct) = if (guessednumber == hd sequence) (io, correct+1) (io <<< "Incorrect...\n", correct)
42 = play (snd (freadline io)) seqname sequence (currentnum + 1) correct
43
44 Start :: *World -> *World
45 Start world
46 # (inputfilepath, world) = selectInputFile world
47 | isNothing inputfilepath = abort "Please select a file"
48 # (sequence, sequencename, world) = loadSeq (fromJust inputfilepath) world
49 # (io, world) = stdio world
50 # io = (play io sequencename sequence 0 0) <<< "Press any key to close"
51 # (_, world) = fclose io world
52 = world