1
1
module List where
2
2
import Control.Applicative
3
3
import Prelude hiding ((++) , reverse , map , zip , sum , any , all , filter , length , maybe )
4
+ import Data.Char -- strings 'n' stuff
5
+
6
+ -- implementation of parseInt
7
+ parseInt :: String -> Int
8
+ parseInt (' -' : cs) = negate $ parseInt cs
9
+ parseInt cs = foldl (\ t c -> 10 * t + digitToInt c) 0 cs
4
10
5
11
-- implementation of (++)
6
12
(++) :: [a ] -> [a ] -> [a ]
7
- (x: xs) ++ ys = x: (xs ++ ys)
8
- [] ++ ys = ys
13
+ (++) (x: xs) = (:) x $ (++) xs
14
+ (++) [] = id
15
+
16
+ -- implementation of intercalate and intersperse
17
+ inter :: (a -> b -> b ) -> b -> a -> [a ] -> b
18
+ inter _ i _ [] = i
19
+ inter f i _ [x] = f x i -- coerce x into a member of type b
20
+ inter f i s (x: xs) = f x $ f s $ inter f i s xs
21
+
22
+ intercalate :: [a ] -> [[a ]] -> [a ]
23
+ intercalate = inter (++) []
9
24
10
- -- implementation of python's join
11
- join :: String -> [String ] -> String
12
- join s [] = " "
13
- join s (x: [] ) = x
14
- join s (x: xs) = x ++ s ++ (join s xs)
25
+ intersperse :: a -> [a ] -> [a ]
26
+ intersperse = inter (:) []
15
27
16
28
-- implementation of reverse
17
29
reverse :: [a ] -> [a ]
@@ -44,9 +56,32 @@ enumerate :: [a] -> [(Int,a)]
44
56
enumerate = zip [0 .. ]
45
57
46
58
-- implementation of slice (list[a:b] in python)
47
- slice :: [a ] -> Int -> Int -> [a ]
48
- slice xs a b = take (b - a) $ drop a xs
49
-
59
+ slice :: Int -> Int -> [a ] -> [a ]
60
+ slice a b = take (b - a) . drop a
61
+
62
+ -- insert element at the first point where preticate is True
63
+ insertif :: (a -> a -> Bool ) -> a -> [a ] -> [a ]
64
+ insertif p i (x: xs)
65
+ | p i x = i : x : xs
66
+ | otherwise = x : insertif p i xs
67
+ insertif _ _ [] = []
68
+
69
+ -- insert each element, at the first point where the preticate is True, in order
70
+ injectif :: (a -> a -> Bool ) -> [a ] -> [a ] -> [a ]
71
+ injectif p (i: is) (x: xs)
72
+ | p i x = i : injectif p is (x: xs)
73
+ | otherwise = x : injectif p (i: is) xs
74
+ injectif _ [] xs = xs
75
+ injectif _ _ [] = []
76
+
77
+ -- implementation of next
78
+ next x (i: y: ys) -- take the first two items in the list
79
+ | x == i = -- if the first item == x,
80
+ y : next x (y: ys) -- take the second, and continue to the rest of the list (minus the first element)
81
+ | otherwise = -- not equal,
82
+ next x (y: ys) -- so skip that element
83
+ next _ [_] = [] -- if there's no second element, then stop
84
+ next _ _ = [] -- if the list is empty, stop
50
85
-- implementations of fold
51
86
foldr' :: (b -> a -> a ) -> a -> [b ] -> a
52
87
foldr' _ i [] = i
@@ -90,19 +125,21 @@ fromMaybe = (`maybe` id)
90
125
91
126
-- function to floop a list of maybes into a maybe of a list. yeah, not a great description
92
127
floop :: (Foldable t , Alternative t ) => t (Maybe a ) -> Maybe (t a )
93
- floop xs = let
94
- f :: (Foldable t , Alternative t ) => Maybe a -> Maybe (t a ) -> Maybe (t a )
95
- f Nothing _ = Nothing
96
- f (Just x) ys = fmap ((<|>) $ pure x) ys
97
- -- f x ys = maybe Nothing (flip (fmap . (<|>) . pure) ys) x -- is equivalent
98
- in
99
- foldr f (Just empty) xs
128
+ floop xs =
129
+ let
130
+ f :: (Foldable t , Alternative t ) => Maybe a -> Maybe (t a ) -> Maybe (t a )
131
+ f Nothing = const Nothing
132
+ f (Just x) = fmap ((<|>) $ pure x)
133
+ -- f x ys = maybe Nothing (flip (fmap . (<|>) . pure) ys) x -- is equivalent
134
+ in
135
+ foldr f (Just empty) xs
100
136
101
137
-- simpler, more limited example
102
138
floop' :: [Maybe a ] -> Maybe [a ]
103
139
floop' [] = Just []
104
140
floop' (Nothing : _) = Nothing
105
- floop' (Just x: xs) = fmap (x: ) $ floop xs
141
+ floop' (Just x: xs) = fmap (x: ) . floop $ xs
142
+ -- floop' xs = foldr (fmap . (:)) (Just []) xs
106
143
107
144
-- floop'' :: (Foldable t, Alternative t) => t (Maybe a) -> Maybe (t a)
108
145
-- floop'' xs = foldr (\x ys -> maybe Nothing (flip (fmap . (<|>) . pure) ys) x) (Just empty) xs --needlessly equivalent
@@ -113,7 +150,6 @@ sequence' = foldr inject (pure empty)
113
150
where inject = liftA2 prepend
114
151
prepend = (<|>) . pure
115
152
116
- -- floop' xs = foldr (fmap . (:)) (Just []) xs
117
153
-- apply each function with the given argument
118
154
applyAll :: [(a -> b )] -> a -> [b ]
119
155
applyAll fs x = map ($ x) fs
0 commit comments