-
Notifications
You must be signed in to change notification settings - Fork 3
/
Chapter_11_my_note.hs
112 lines (79 loc) · 3.08 KB
/
Chapter_11_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
module Chapter_11_my_note where
import Test.QuickCheck
import Data.Function
infixl 9 >.>
(>.>) :: (a -> b) -> (b -> c) -> (a -> c)
g >.> f = f . g
composeList :: [a -> a] -> (a -> a)
composeList = foldr (.) id
mapFuns :: [a -> b] -> a -> [b]
mapFuns fs val = map (\f -> f val) fs
comp2 :: (a -> b) -> (b -> b -> c) -> (a -> a -> c)
comp2 f g = \x y -> g (f x) (f y)
isBlank :: Char -> Bool
isBlank = \ch -> not (ch `elem` "\t\n")
total :: (Integer -> Integer) -> (Integer -> Integer)
total f = \n -> foldr ((+) . f) 0 [0 .. n]
total2 :: (Integer -> Integer) -> (Integer -> Integer)
total2 f = sum . map f . \n -> [0 .. n]
invseq :: (a -> b -> c) -> (b -> a -> c)
invseq f = \v2 v1 -> f v1 v2
mapFuns2 :: [a -> b] -> a -> [b]
mapFuns2 fs val = map ($ val) fs
_curry :: ((a, b) -> c) -> a -> b -> c
_curry f = \x y -> f (x, y)
_uncurry :: (a -> b -> c) -> (a, b) -> c
_uncurry f = \(x, y) -> f x y
_anotherCurry :: ((a, b) -> c) -> a -> b -> c
_anotherCurry f x y = f (x, y)
_anotherUncurry :: (a -> b -> c) -> (a, b) -> c
_anotherUncurry f (x, y) = f x y
curry3 :: ((a, b, c) -> d) -> a -> b -> c -> d
curry3 f x y z = f (x, y, z)
uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 f (x, y, z) = f x y z
curryList :: ([a] -> d) -> a -> [a] -> d
curryList f val ls = f (val : ls)
uncurryList :: (a -> [a] -> d) -> [a] -> d
uncurryList f ls = f (head ls) (tail ls)
prop_uncurryZip_unzip :: Eq a => Eq b => ([a], [b]) -> Property
prop_uncurryZip_unzip tester@ (lsa, lsb) =
(length lsa == length lsb) ==>
(unzip . uncurry zip) tester == tester
_prop_uncurryZip_unzip :: Eq a => Eq b => ([a], [b]) -> Bool
_prop_uncurryZip_unzip tester@ (lsa, lsb) = if length lsa == length lsb
then (unzip . uncurry zip) tester == tester
else True
twice :: (a -> a) -> (a -> a)
twice f = f . f
_succ :: Integer -> Integer
_succ a = a + 1
iter :: Integer -> (a -> a) -> (a -> a)
iter n f
| n > 0 = f . iter (n - 1) f
| otherwise = id
double :: Integer -> Integer
double = (* 2)
_sq :: Integer -> Integer
_sq d = d * d
_iter :: Integer -> (a -> a) -> (a -> a)
_iter n f = foldr (.) id (replicate (fromInteger n :: Int) f)
__iter :: Integer -> (a -> a) -> (a -> a)
__iter n f = foldr (.) id (listcreate n f)
listcreate :: Integer -> a -> [a]
listcreate = fix (\f n p -> if n > 0
then p : f (n - 1) p
else [])
_iter_ :: Integer -> (a -> a) -> (a -> a)
_iter_ n f = foldr (.) id [f | _ <- [1 .. n]]
getEvens :: [Integer] -> [Integer]
getEvens = filter ((== 0) . (`mod` 2))
mapFuns3 :: [a -> b] -> a -> [b]
mapFuns3 fs val = map (applyVal val) fs
where
applyVal :: a -> (a -> b) -> b
applyVal val' f = f val'
slope :: (Float -> Float) -> (Float -> Float)
slope f = \x -> (f (x + 1.0e-5) - f (x - 1.0e-5)) / 2.0e-5
integrate :: (Float -> Float) -> (Float -> Float -> Float)
integrate f x y = sum $ map ((* 1.0e-5) . f) [x, x + 1.0e-5 .. y]