-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicative.hs
More file actions
58 lines (44 loc) · 1.54 KB
/
Applicative.hs
File metadata and controls
58 lines (44 loc) · 1.54 KB
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
import Text.Read
-- demo fmap over Maybe Double
demo1 = do
l <- getLine
let maybeDouble = fmap (*2) (readMaybe l :: Maybe Double)
case maybeDouble of
Just d -> putStrLn (show d)
otherwise -> putStrLn "nothing"
-- demo applicative: apply function to Maybe and then apply another Maybe
demo2 = do
l1 <- getLine
l2 <- getLine
let d1 = readMaybe l1 :: Maybe Double
d2 = readMaybe l2
let maybeSum = fmap (+) d1 <*> d2
case maybeSum of
Just s -> putStrLn (show s)
otherwise -> putStrLn "nothing"
demo2' = do
d1 <- fmap readMaybe getLine
d2 <- fmap readMaybe getLine
let maybeSum = (+) <$> d1 <*> d2 :: Maybe Double
case maybeSum of
Just d -> putStrLn (show d)
otherwise -> putStrLn "nothing"
-- demo appliative: apply concatination function to IO String and then apply another IO String
demo3 = do
s1 <- getLine
s2 <- getLine
putStrLn (s1 ++ s2)
demo3' = do
s <- (++) <$> getLine <*> getLine
putStrLn s
-- demo applicative: sequencing operator *>
demo4 = (\_ y -> y) <$> putStrLn "First" <*> putStrLn "Second"
demo4' = putStrLn "First" *> putStrLn "Second"
-- demo applicative: sequencing operator *> in withing do notation
demo5 = do
putStrLn "choose 2 strings"
s <- (++) <$> getLine <*> getLine
putStrLn s
demo5' = do
s <- putStrLn "choose 2 strings" *> ((++) <$> getLine <*> getLine)
putStrLn s