-
Notifications
You must be signed in to change notification settings - Fork 3
/
Chapter_18_my_note.hs
147 lines (117 loc) · 5.11 KB
/
Chapter_18_my_note.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{-# LANGUAGE FlexibleInstances #-}
module Chapter_18_my_note where
import System.IO
import System.IO.Unsafe (unsafePerformIO)
import Chapter_17_my_note ( Parse (..)
, none
, succeed )
import qualified Control.Monad.Identity as I
import qualified Data.List as L
readAndWrite :: IO ()
readAndWrite = do line <- getLine
putStrLn "Get one line"
putStrLn line
readEcho :: IO ()
readEcho = do putStrLn "Give me a line"
line <- getLine
putStrLn ("the line we get is: " ++ line)
sumInts :: Integer -> IO Integer
sumInts s = do temp <- getLine
let n = read temp :: Integer
if n == 0
then return s
else sumInts (s + n)
sumInteract :: IO ()
sumInteract = do putStrLn "Enter Integers one per line"
putStrLn "These will be summed until zero occurred"
sum_val <- sumInts 0
putStrLn ("The final sum is " ++ show sum_val)
_fmap :: (a -> b) -> IO a -> IO b
_fmap f origio = do val <- origio
return (f val)
repeater :: IO Bool -> IO () -> IO ()
repeater test oper = do checker <- test
if checker
then return ()
else do oper
repeater test oper
whileG :: (a -> IO Bool) -> (a -> IO a) -> (a -> IO a)
whileG test oper val = do goOrStop <- test val
if goOrStop
then return val
else do operated <- oper val
whileG test oper operated
improvedSumer :: (Integer, Integer) -> IO (Integer, Integer)
improvedSumer (times, n) = do putStrLn "give me a number: "
temp <- getLine
return (times - 1, n + read temp :: Integer)
controller :: (Integer, Integer) -> IO Bool
controller (val, _) = return (val <= 0)
getAverage :: IO ()
getAverage = do putStrLn "Give me a integer for average number: "
tempAvgNum <- getLine
let avgNum = read tempAvgNum :: Integer
(_, sumVal) <- whileG controller improvedSumer (avgNum, 0)
let sumValFloat = fromInteger sumVal :: Float
let avgNumFloat = fromInteger avgNum :: Float
let average = sumValFloat / avgNumFloat
putStrLn ("the average you want is: " ++ show average)
accumulate :: [IO a] -> IO [a]
accumulate = return . map unsafePerformIO
sequence :: [IO a] -> IO ()
sequence iolist = do _ <- accumulate iolist
return ()
seqFuns :: [a -> IO a] -> (a -> a)
seqFuns funs iniv = foldr subfunc iniv funs
where subfunc :: (a -> IO a) -> a -> a
subfunc func = unsafePerformIO . func
seqList :: [a -> IO a] -> a -> IO a
seqList funcs val = return (seqFuns funcs val)
copyInteract :: IO ()
copyInteract = do hSetBuffering stdin LineBuffering
copyEOF
hSetBuffering stdin NoBuffering
copyEOF :: IO ()
copyEOF = do eof <- isEOF
if eof
then return ()
else do line <- getLine
putStrLn line
copyEOF
combination :: [a -> a] -> (a -> a)
combination funcs valfeed = foldr (\f x -> f x) valfeed funcs
promptReadFile :: IO ()
promptReadFile = do loca <- getLine
(putStrLn . unsafePerformIO . readFile) loca
listIOprog :: String -> String
listIOprog = unlines . map reverse . lines
sumIntsFile :: IO ()
sumIntsFile = do loca <- getLine
let contents = (lines . unsafePerformIO . readFile) loca
let values = map (\x -> read x :: Int) contents
let result = sum values
print result
promptReadFileBind :: IO ()
promptReadFileBind = getLine >>= (putStrLn . unsafePerformIO . readFile)
sumIntsFileBind :: IO ()
sumIntsFileBind = getLine >>= process
where process :: String -> IO ()
process = print . sum . map (\x -> read x :: Int)
. lines . unsafePerformIO . readFile
addOneLineParse :: IO ()
addOneLineParse = getLine >>= \line -> (putStrLn . show) (1 + read line :: Int)
(>@>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
f >@> g = \x -> f x >>= g
newtype MP a b = MP { mp :: Parse a b }
phoneBook :: [(String, String)]
phoneBook = [ ("betty", "number1")
, ("bonnie", "number2")
, ("pasty", "number3")
, ("lucille", "number4")
, ("wendy", "number5")
, ("penny", "number6")
, ("wendy", "number7") ]
findKey :: (Eq k) => k -> [(k, v)] -> Maybe v
findKey key = foldr (\(ck, cv) t -> if ck == key then Just cv else t) Nothing
findKey_ :: (Eq k) => k -> [(k, v)] -> Maybe v
findKey_ key = foldl (\t (ck, cv) -> if ck == key then Just cv else t) Nothing