1 2 -- ==========================================================-- 3 -- === Various useful bits & pieces ===-- 4 -- === MyUtils.hs ===-- 5 -- ==========================================================-- 6 7 module MyUtils where 8 import BaseDefs 9 10 infixl 9 ## 11 12 -- ==========================================================-- 13 -- 14 myFail msg 15 = error ("\n" ++ msg ++ "\n") 16 17 panic msg 18 = error ("\nPanic! (the `impossible' happened):\n" ++ msg ++ "\n") 19 20 21 -- ==========================================================-- 22 -- 23 mySubtract :: Int -> Int -> Int 24 25 mySubtract x y = y - x 26 27 28 -- ==========================================================-- 29 -- 30 myZipWith2 :: (a -> b -> c) -> [a] -> [b] -> [c] 31 32 myZipWith2 f [] [] = [] 33 myZipWith2 f (a:as) (b:bs) = f a b : myZipWith2 f as bs 34 myZipWith2 _ _ _ = panic "myZipWith2: unequal lists" 35 36 myZip2 = myZipWith2 (\a b -> (a, b)) 37 38 39 -- ==========================================================-- 40 -- 41 myZipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] 42 43 myZipWith3 f [] [] [] = [] 44 myZipWith3 f (a:as) (b:bs) (c:cs) = f a b c : myZipWith3 f as bs cs 45 myZipWith3 _ _ _ _ = panic "myZipWith3: unequal lists" 46 47 myZip3 = myZipWith3 (\a b c -> (a, b, c)) 48 49 50 -- ==========================================================-- 51 -- 52 myZipWith4 :: (a -> b -> c -> d -> e) -> [a] -> [b] -> [c] -> [d] -> [e] 53 54 myZipWith4 f [] [] [] [] = [] 55 myZipWith4 f (a:as) (b:bs) (c:cs) (d:ds) = f a b c d : myZipWith4 f as bs cs ds 56 myZipWith4 _ _ _ _ _ = panic "myZipWith4: unequal lists" 57 58 myZip4 = myZipWith4 (\a b c d -> (a, b, c, d)) 59 60 61 -- ==========================================================-- 62 -- 63 myZipWith5 :: (a -> b -> c -> d -> e -> f) -> 64 [a] -> [b] -> [c] -> [d] -> [e] -> [f] 65 66 myZipWith5 f [] [] [] [] [] = [] 67 myZipWith5 f (a:as) (b:bs) (c:cs) (d:ds) (e:es) 68 = f a b c d e : myZipWith5 f as bs cs ds es 69 myZipWith5 _ _ _ _ _ _ 70 = panic "myZipWith5: unequal lists" 71 72 myZip5 = myZipWith5 (\a b c d e -> (a, b, c, d, e)) 73 74 75 -- ==========================================================-- 76 -- 77 myAndWith2 :: (a -> b -> Bool) -> [a] -> [b] -> Bool 78 79 myAndWith2 f [] [] 80 = True 81 82 myAndWith2 f (a:as) (b:bs) 83 = if f a b 84 then myAndWith2 f as bs 85 else False 86 87 myAndWith2 _ _ _ 88 = panic "myAndWith2: unequal lists" 89 90 91 -- ==========================================================-- 92 -- 93 myAny, myAll :: (a -> Bool) -> [a] -> Bool 94 95 myAny p [] = False 96 myAny p (x:xs) = if p x then True else myAny p xs 97 98 myAll p [] = True 99 myAll p (x:xs) = if p x then myAll p xs else False 100 101 102 -- ==========================================================-- 103 -- 104 myAnd, myOr :: [Bool] -> Bool 105 106 myAnd [] = True 107 myAnd (x:xs) = if x then myAnd xs else False 108 109 myOr [] = False 110 myOr (x:xs) = if x then True else myOr xs 111 112 113 -- ==========================================================-- 114 -- 115 myListVariants :: [a] -> [[a]] -> [[a]] 116 117 myListVariants [] [] = [] 118 119 myListVariants (x:xs) (rs:rss) 120 = map ((flip (:)) xs) rs ++ map (x:) (myListVariants xs rss) 121 122 myListVariants _ _ = panic "myListVariants: unequal lists" 123 124 125 -- ==========================================================-- 126 -- 127 myCartesianProduct :: [[a]] -> [[a]] 128 129 myCartesianProduct [] 130 = [[]] 131 132 myCartesianProduct (xs:xss) 133 = let g as bs = map (:bs) as 134 in 135 concat (map (g xs) (myCartesianProduct xss)) 136 137 138 -- ==========================================================-- 139 -- 140 mySeq :: (Eq a) => a -> b -> b 141 142 mySeq x y | x == x = y 143 144 145 -- ==========================================================-- 146 -- 147 myIntsFromTo :: Int -> Int -> [Int] 148 149 myIntsFromTo n m 150 = if n > m 151 then [] 152 else n : myIntsFromTo (n + (1 :: Int)) m 153 154 155 -- ==========================================================-- 156 -- 157 myIntsFrom :: Int -> [Int] 158 159 myIntsFrom n = n : myIntsFrom (n + (1 :: Int)) 160 161 162 -- ==========================================================-- 163 -- 164 (##) :: [b] -> Int -> b 165 166 [] ## n 167 = panic "(##) (1)" 168 (x:xs) ## n 169 = if n == (0 :: Int) then x else xs ## (n - (1 :: Int)) 170 171 172 -- ==========================================================-- 173 -- === end MyUtils.hs ===-- 174 -- ==========================================================--