-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaskell_2.hs
More file actions
134 lines (95 loc) · 2.81 KB
/
Copy pathhaskell_2.hs
File metadata and controls
134 lines (95 loc) · 2.81 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
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
import Prelude hiding ((.))
twice :: (a -> a) -> a -> a
--high order function- takes a function as 1st argument
twice f x = f (f x)
map' :: (a -> b) -> [a] -> [b]
map' f xs = [f x | x <- xs]
map'' :: (a -> b) -> [a] -> [b]
map'' f [] = []
map'' f (x:xs) = f x : map'' f xs
filter' :: (a -> Bool) -> [a] -> [a]
filter' f xs = [x | x <- xs, f x]
filter'' :: (a -> Bool) -> [a] -> [a]
filter'' f [] = []
filter'' f (x:xs) = if f x then x : filter'' f xs else filter'' f xs
filter''' :: (a -> Bool) -> [a] -> [a]
filter''' f [] = []
filter''' f (x:xs)
| f x = x : filter''' f xs
| otherwise = filter''' f xs
sum' :: [Integer] -> Integer
sum' [] = 0
sum' (x:xs) = x + sum' xs
product' :: [Integer] -> Integer
product' [] = 1
product' (x:xs) = x * product' xs
and' :: [Bool] -> Bool
and' [] = True
and' (x:xs) = x && and' xs
foldr' :: (a -> b -> b) -> b -> [a] -> b
foldr' f v [] = v
foldr' f v (x:xs) = f x (foldr' f v xs)
sum'' = foldr' (+) 0
product'' = foldr' (*) 1
and'' = foldr' (&&) True
-- product' [1,2,3]
-- foldr' (*) 1 [1,2,3]
-- foldr' (*) 1 (1:(2:(3:[])))
-- zamieniamy każdy : na f (czyli *) i każdy [] na v (czyli 1)
-- (1*(2*(3*1)))
-- 6
length' :: [a] -> Int
length' [] = 0
length' (_:xs) = 1 + length' xs
length'' = foldr' (\_ n -> 1 + n) 0
reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
reverse'' = foldr' (\x xs -> xs ++ [x]) []
map''' f = foldr' (\x xs -> f x : xs) []
filter'''' f = foldr' (\x xs -> if f x then x : xs else xs) []
(.) :: (b -> c) -> (a -> b) -> (a -> c)
f . g = \x -> f (g x)
odd' :: Int -> Bool
odd' = not . even
all' :: (a -> Bool) -> [a] -> Bool
all' f [] = True
all' f (a:as) = f a && all' f as
all'' f = foldr' (\x xs -> f x && xs) True
all''' f xs = and [f x | x <- xs]
takewhile :: (a -> Bool) -> [a] -> [a]
takewhile f [] = []
takewhile f (x:xs)
| f x = x : takewhile f xs
| otherwise = []
dropwhile :: (a -> Bool) -> [a] -> [a]
dropwhile f [] = []
dropwhile f (x:xs)
| f x = dropwhile f xs
| otherwise = x : xs
mapl f = foldl (\ xs x -> xs ++ [f x]) []
dec2int = foldl (\ x y -> 10 * x + y) 0
-- dec2int [1,2,3,4] = 1234
compose :: [([a] -> [a])] -> ([a] -> [a])
compose = foldl (.) id
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
unfold :: (b -> Bool) -> (b -> a) -> (b -> b) -> b -> [a]
unfold p h t x
| p x = []
| otherwise = h x : unfold p h t (t x)
type Bit = Int
int2bin :: Int -> [Bit]
int2bin 0 = []
int2bin n = n `mod` 2 : int2bin (n `div` 2)
int2bin' = unfold (==0) (`mod` 2) (`div` 2)
chop8 :: [Bit] -> [[Bit]]
chop8 [] = []
chop8 bits = take 8 bits : chop8 (drop 8 bits)
chop8' = unfold null (take 8) (drop 8)
map :: (a -> b) -> [a] -> [b]
map f = unfold null (f . head) tail
iterate' :: (a -> a) -> a -> [a]
iterate' f = unfold (const False) id f