1
1
module List where
2
2
import Control.Applicative
3
- import Prelude hiding ((++) , reverse , map , zip , sum , any , all , filter , length , maybe )
4
- import Data.Char -- strings 'n' stuff
3
+ import Prelude hiding ((++) , reverse , map , zip , sum , any , all , filter , length , maybe , elem )
4
+ import Data.Char (digitToInt )
5
+ import Data.Universe.Helpers (diagonal )
6
+ import Data.List (nub )
5
7
6
8
-- implementation of parseInt
7
9
parseInt :: String -> Int
@@ -36,10 +38,10 @@ map f [] = []
36
38
map f (x: xs) = (f x): (map f xs)
37
39
38
40
-- implementations of range
39
- range :: Int -> Int -> [Int ]
41
+ range :: Integral n => n -> n -> [n ]
40
42
range a b = [a.. b]
41
43
42
- range' :: Int -> Int -> [Int ]
44
+ range' :: Integral n => n -> n -> [n ]
43
45
range' a b
44
46
| a < b = (a : (range' (a+ 1 ) b ))
45
47
| a == b = [a]
@@ -52,7 +54,7 @@ zip [] _ = []
52
54
zip _ [] = []
53
55
54
56
-- implementation of python's enumerate
55
- enumerate :: [a ] -> [(Int ,a )]
57
+ enumerate :: Integral n => [a ] -> [(n ,a )]
56
58
enumerate = zip [0 .. ]
57
59
58
60
-- implementation of slice (list[a:b] in python)
@@ -82,6 +84,7 @@ next x (i:y:ys) -- take the first two items in the list
82
84
next x (y: ys) -- so skip that element
83
85
next _ [_] = [] -- if there's no second element, then stop
84
86
next _ _ = [] -- if the list is empty, stop
87
+
85
88
-- implementations of fold
86
89
foldr' :: (b -> a -> a ) -> a -> [b ] -> a
87
90
foldr' _ i [] = i
@@ -102,27 +105,64 @@ all f = foldr ((&&).f) True
102
105
any :: Foldable t => (a -> Bool ) -> t a -> Bool
103
106
any f = foldr ((||) . f) False
104
107
108
+ -- implementation of elem
109
+ elem :: (Eq a , Foldable t ) => a -> t a -> Bool
110
+ elem x = any (== x)
111
+
112
+ -- basic list intersection. ineffective on infinite lists
113
+ intersect :: Eq a => [a ] -> [a ] -> [a ]
114
+ intersect (x: xs) ys
115
+ | elem x ys = x : intersect xs ys
116
+ | otherwise = intersect xs ys
117
+
118
+ isect :: Eq a => [a ] -> [a ] -> [a ]
119
+ isect xs = catMaybes . diagonal . map matches
120
+ where matches y = [if x == y then Just x else Nothing | x <- xs] -- ensures that non-yields are interleaved with yields
121
+
122
+ -- mFilter :: (a -> Bool) -> [a] -> [Maybe a]
123
+ -- mFilter f xs = [if f x then Just x else Nothing | x <- xs]
124
+
125
+ -- mFilter :: (a -> Bool) -> [a] -> [Maybe a]
126
+ -- mFilter f (x:xs)
127
+ -- | f x = Just x : mFilter f xs
128
+ -- | otherwise = Nothing : mFilter f xs
129
+
130
+ boolMaybe :: (a -> Bool ) -> a -> Maybe a
131
+ boolMaybe f x
132
+ | f x = Just x
133
+ | otherwise = Nothing
134
+
135
+ mFilter :: (a -> Bool ) -> [a ] -> [Maybe a ]
136
+ mFilter = map . boolMaybe
137
+
105
138
-- implementation of filter
106
139
filter :: (a -> Bool ) -> [a ] -> [a ]
107
140
filter _ [] = []
108
141
filter f (x: xs)
109
- | f x = x: ( filter f xs)
142
+ | f x = x : filter f xs
110
143
| otherwise = filter f xs
111
144
112
145
-- implementation of length
113
- length :: Foldable t => t a -> Int
146
+ length :: ( Integral n , Foldable t ) => t a -> n
114
147
length = foldr ((+) . (const 1 )) 0
115
- length' :: [ a ] -> Int
116
- length' = sum . (map $ const 1 )
148
+ length' :: ( Integral n , Functor t , Foldable t ) => t a -> n
149
+ length' = sum . (fmap $ const 1 )
117
150
118
- -- implementation of maybe and fromMaybe
151
+ -- implementation of maybe, fromMaybe, and catMaybes
119
152
maybe :: a -> (b -> a ) -> (Maybe b ) -> a
120
153
maybe d _ Nothing = d
121
154
maybe _ f (Just x) = f x
122
155
123
156
fromMaybe :: a -> Maybe a -> a
124
157
fromMaybe = (`maybe` id )
125
158
159
+ catMaybes :: (Foldable t , Alternative t ) => t (Maybe a ) -> t a
160
+ catMaybes = foldr
161
+ (\ x -> case x of
162
+ Just x -> (pure x <|> )
163
+ Nothing -> id
164
+ ) empty
165
+
126
166
-- function to floop a list of maybes into a maybe of a list. yeah, not a great description
127
167
floop :: (Foldable t , Alternative t ) => t (Maybe a ) -> Maybe (t a )
128
168
floop xs =
@@ -154,10 +194,6 @@ sequence' = foldr inject (pure empty)
154
194
applyAll :: [(a -> b )] -> a -> [b ]
155
195
applyAll fs x = map ($ x) fs
156
196
157
- -- testing out monads and bind
158
- mayadd :: Maybe Int -> Maybe Int -> Maybe Int
159
- mayadd ma mb = ma >>= (\ a -> mb >>= (\ b -> Just (a + b)))
160
-
161
197
main = do
162
198
mapM_ print [" hello" ++ " world" == " hello world"
163
199
,reverse " nope" == " epon"
@@ -169,9 +205,6 @@ main = do
169
205
,range' 5 1 == []
170
206
,zip [1 ,2 ,3 ] [4 ,5 ,6 ,7 ] == [(1 ,4 ),(2 ,5 ),(3 ,6 )]
171
207
,zip [1 ,2 ,3 ,7 ] [4 ,5 ,6 ] == [(1 ,4 ),(2 ,5 ),(3 ,6 )]
172
- ,mayadd (Just 1 ) (Just 2 ) == Just 3
173
- ,mayadd Nothing (Just 2 ) == Nothing
174
- ,mayadd (Just 1 ) Nothing == Nothing
175
208
,foldl (-) 0 [1 ,2 ,3 ,4 ] == - 10 -- (((1 - 2) - 3) - 4) - 0
176
209
,foldl' (-) 0 [1 ,2 ,3 ,4 ] == - 10
177
210
,foldr (-) 0 [1 ,2 ,3 ,4 ] == - 2 -- 1 - (2 - (3 - (4 - 0)))
0 commit comments