-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.hs
executable file
·49 lines (37 loc) · 1.31 KB
/
test.hs
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
data Test a = Test (TestS -> (a,TestS))
instance Monad Test where
return a = Test (\s -> (a,s))
Test f >>= gm = Test (\s0 -> let (x,s1) = f s0
(Test g) = gm x
in g s1)
insertList l = Test (\s0 -> ((),l))
getElement n = Test (\(l) -> (l!!n,l))
runTest :: TestS -> Test a -> a
runTest s0 (Test c) = fst $ c s0
testcode = do h <- getElement 5
p <- getElement 4
t <- getElement 2
return p
test = runTest [0..] testcode
{- guards
g x y = case 0 of
_ | x == 1 && y>2 -> 1
| x > 2 -> 3
| otherwise -> 2
Given a function with guards with otherwise
f <patterns> | p1 = e1
| p2 = e2
| ... ...
| pn = en
| otherwise = eo
can be converted like this
f <patterns> = if p1 then e1
else if p2 then e2
else eo
in case there is no otherwise i'll generate a pattern matching error
at the end with a function that always fails and returns the same type
f <patterns> = if p1 then e1
else if p2 then e2
else ((\a -> case a of 0 -> ex) 1)
ex is an expression choosen from {e1,..,en}
-}