Skip to content

Commit e3d1f1c

Browse files
committed
added infinite list intersection, elem
1 parent 076e904 commit e3d1f1c

File tree

1 file changed

+50
-17
lines changed

1 file changed

+50
-17
lines changed

List.hs

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module List where
22
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)
57

68
-- implementation of parseInt
79
parseInt :: String -> Int
@@ -36,10 +38,10 @@ map f [] = []
3638
map f (x:xs) = (f x):(map f xs)
3739

3840
-- implementations of range
39-
range :: Int -> Int -> [Int]
41+
range :: Integral n => n -> n -> [n]
4042
range a b = [a..b]
4143

42-
range' :: Int -> Int -> [Int]
44+
range' :: Integral n => n -> n -> [n]
4345
range' a b
4446
| a < b = (a : (range' (a+1) b ))
4547
| a == b = [a]
@@ -52,7 +54,7 @@ zip [] _ = []
5254
zip _ [] = []
5355

5456
-- implementation of python's enumerate
55-
enumerate :: [a] -> [(Int,a)]
57+
enumerate :: Integral n => [a] -> [(n,a)]
5658
enumerate = zip [0..]
5759

5860
-- 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
8284
next x (y:ys) -- so skip that element
8385
next _ [_] = [] -- if there's no second element, then stop
8486
next _ _ = [] -- if the list is empty, stop
87+
8588
-- implementations of fold
8689
foldr' :: (b -> a -> a) -> a -> [b] -> a
8790
foldr' _ i [] = i
@@ -102,27 +105,64 @@ all f = foldr ((&&).f) True
102105
any :: Foldable t => (a -> Bool) -> t a -> Bool
103106
any f = foldr ((||).f) False
104107

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+
105138
-- implementation of filter
106139
filter :: (a -> Bool) -> [a] -> [a]
107140
filter _ [] = []
108141
filter f (x:xs)
109-
| f x = x:(filter f xs)
142+
| f x = x : filter f xs
110143
| otherwise = filter f xs
111144

112145
-- implementation of length
113-
length :: Foldable t => t a -> Int
146+
length :: (Integral n, Foldable t) => t a -> n
114147
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)
117150

118-
-- implementation of maybe and fromMaybe
151+
-- implementation of maybe, fromMaybe, and catMaybes
119152
maybe :: a -> (b -> a) -> (Maybe b) -> a
120153
maybe d _ Nothing = d
121154
maybe _ f (Just x) = f x
122155

123156
fromMaybe :: a -> Maybe a -> a
124157
fromMaybe = (`maybe` id)
125158

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+
126166
-- function to floop a list of maybes into a maybe of a list. yeah, not a great description
127167
floop :: (Foldable t, Alternative t) => t (Maybe a) -> Maybe (t a)
128168
floop xs =
@@ -154,10 +194,6 @@ sequence' = foldr inject (pure empty)
154194
applyAll :: [(a -> b)] -> a -> [b]
155195
applyAll fs x = map ($x) fs
156196

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-
161197
main = do
162198
mapM_ print ["hello" ++ " world" == "hello world"
163199
,reverse "nope" == "epon"
@@ -169,9 +205,6 @@ main = do
169205
,range' 5 1 == []
170206
,zip [1,2,3] [4,5,6,7] == [(1,4),(2,5),(3,6)]
171207
,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
175208
,foldl (-) 0 [1,2,3,4] == -10 -- (((1 - 2) - 3) - 4) - 0
176209
,foldl' (-) 0 [1,2,3,4] == -10
177210
,foldr (-) 0 [1,2,3,4] == -2 -- 1 - (2 - (3 - (4 - 0)))

0 commit comments

Comments
 (0)