Skip to content

Commit 83cac9e

Browse files
committed
added some functions, eta-reduced some functions, renamed some functions to reflect name in standard libraries
1 parent 39c3919 commit 83cac9e

File tree

4 files changed

+70
-33
lines changed

4 files changed

+70
-33
lines changed

List.hs

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
module List where
22
import Control.Applicative
33
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
410

511
-- implementation of (++)
612
(++) :: [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 (++) []
924

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 (:) []
1527

1628
-- implementation of reverse
1729
reverse :: [a] -> [a]
@@ -44,9 +56,32 @@ enumerate :: [a] -> [(Int,a)]
4456
enumerate = zip [0..]
4557

4658
-- 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
5085
-- implementations of fold
5186
foldr' :: (b -> a -> a) -> a -> [b] -> a
5287
foldr' _ i [] = i
@@ -90,19 +125,21 @@ fromMaybe = (`maybe` id)
90125

91126
-- function to floop a list of maybes into a maybe of a list. yeah, not a great description
92127
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
100136

101137
-- simpler, more limited example
102138
floop' :: [Maybe a] -> Maybe [a]
103139
floop' [] = Just []
104140
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
106143

107144
--floop'' :: (Foldable t, Alternative t) => t (Maybe a) -> Maybe (t a)
108145
--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)
113150
where inject = liftA2 prepend
114151
prepend = (<|>) . pure
115152

116-
--floop' xs = foldr (fmap . (:)) (Just []) xs
117153
-- apply each function with the given argument
118154
applyAll :: [(a -> b)] -> a -> [b]
119155
applyAll fs x = map ($x) fs

Math.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fibs :: [Int] -- Fibonacci sequence
44
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
55

66
fib :: Int -> Int -- Fibonacci number
7-
fib n = fibs !! n
7+
fib = (!!) fibs
88

99
hcf :: Int -> Int -> Int -- Highest common factor
1010
hcf 0 y = y
@@ -21,13 +21,14 @@ lb x
2121

2222
lbRnd :: Int -> Maybe Int -- Binary logarithm, rounded nearest
2323
lbRnd 1 = Just 0
24-
lbRnd x = let
25-
m :: Floating b => Int -> b
26-
m x = 2 ** ((fromIntegral x) + 1/2)
27-
mid :: Floating b => Int -> Maybe b
28-
mid x = fmap m (lb x)
29-
in
30-
if fmap ((fromIntegral x)<) (mid x) == Just True then lb x else lbMax x
24+
lbRnd x =
25+
let
26+
m :: Floating b => Int -> b
27+
m x = 2 ** ((fromIntegral x) + 1/2)
28+
mid :: Floating b => Int -> Maybe b
29+
mid x = fmap m (lb x)
30+
in
31+
if fmap ((fromIntegral x)<) (mid x) == Just True then lb x else lbMax x
3132

3233
lbMax :: Int -> Maybe Int -- Binary logarithm, rounded up
3334
lbMax 1 = Just 0

Staq.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Staq where
2-
import List (join)
2+
import List (intercalate)
33
import Control.Applicative (liftA2)
44
-- queue (kinda) efficiently implemented as 2 stacks
55
type Staq a = ([a],[a])
@@ -34,6 +34,6 @@ main = do
3434
print $ q
3535
print $ flush q
3636
let nth q n = iterate (>>= rest) (Just q) !! n
37-
let showStaq q = liftA2 (\d r -> join ":" [show d, show r]) (deq q) (rest q)
37+
let showStaq q = liftA2 (\d r -> intercalate ":" [show d, show r]) (deq q) (rest q)
3838
mapM_ print $ map (\n -> (nth q n) >>= showStaq) [0,1,2,3,4,5]
3939

Tree.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Tree where
22

3-
import List (join)
3+
import List (intercalate)
44

55
newtype PlainString = PlainString String
66
instance Show PlainString where
@@ -9,13 +9,13 @@ instance Show PlainString where
99
data Tree a b = Leaf a | Node b (Tree a b) (Tree a b)
1010

1111
inorder (Leaf x) = show x
12-
inorder (Node a b c) = join " " ["[", (inorder b), show a, (inorder c), "]"]
12+
inorder (Node a b c) = intercalate " " ["[", (inorder b), show a, (inorder c), "]"]
1313

1414
preorder (Leaf x) = show x
15-
preorder (Node a b c) = join " " ["[", show a, (preorder b), (preorder c), "]"]
15+
preorder (Node a b c) = intercalate " " ["[", show a, (preorder b), (preorder c), "]"]
1616

1717
postorder (Leaf x) = show x
18-
postorder (Node a b c) = join " " ["[", (postorder b), (postorder c), show a, "]"]
18+
postorder (Node a b c) = intercalate " " ["[", (postorder b), (postorder c), show a, "]"]
1919

2020
main = do
2121
let tree = Node (PlainString "/") (Leaf 1) (Node (PlainString "+") (Leaf 2) (Leaf 3))

0 commit comments

Comments
 (0)