-
Notifications
You must be signed in to change notification settings - Fork 0
/
YesNo1.hs
63 lines (52 loc) · 1.36 KB
/
YesNo1.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module YesNo1 where
-- weakly typed JS string.length > 0 = True functionality
class YesNo a where
yesno :: a -> Bool
instance YesNo Int where
yesno 0 = False
yesno _ = True
instance YesNo [a] where
yesno [] = False
yesno _ = True
--id? It's just a standard library function that takes a parameter and returns the same thing
instance YesNo Bool where
yesno = id
instance YesNo (Maybe a) where
yesno (Just _) = True
yesno Nothing = False
instance YesNo (Tree a) where
yesno EmptyTree = False
yesno _ = True
instance YesNo TrafficLight where
yesno Red = False
yesno _ = True
-- ghci> yesno $ length []
-- False
-- ghci> yesno "haha"
-- True
-- ghci> yesno ""
-- False
-- ghci> yesno $ Just 0
-- True
-- ghci> yesno True
-- True
-- ghci> yesno EmptyTree
-- False
-- ghci> yesno []
-- False
-- ghci> yesno [0,0,0]
-- True
-- ghci> :t yesno
-- yesno :: (YesNo a) => a -> Bool
yesnoIf :: (YesNo y) => y -> a -> a -> a
yesnoIf yesnoVal yesResult noResult = if yesno yesnoVal then yesResult else noResult
-- ghci> yesnoIf [] "YEAH!" "NO!"
-- "NO!"
-- ghci> yesnoIf [2,3,4] "YEAH!" "NO!"
-- "YEAH!"
-- ghci> yesnoIf True "YEAH!" "NO!"
-- "YEAH!"
-- ghci> yesnoIf (Just 500) "YEAH!" "NO!"
-- "YEAH!"
-- ghci> yesnoIf Nothing "YEAH!" "NO!"
-- "NO!"