]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Misc.hs
Add the all_equal function to the Misc module.
[spline3.git] / src / Misc.hs
1 -- | The Misc module contains helper functions that seem out of place
2 -- anywhere else.
3 module Misc
4 where
5
6
7 -- | The standard factorial function. See
8 -- <http://www.willamette.edu/~fruehr/haskell/evolution.html> for
9 -- possible improvements.
10 factorial :: Int -> Int
11 factorial n
12 | n <= 1 = 1
13 | n > 20 = error "integer overflow in factorial function"
14 | otherwise = product [1..n]
15
16
17 -- | Takes a three-dimensional list, and flattens it into a
18 -- one-dimensional one.
19 flatten :: [[[a]]] -> [a]
20 flatten xs = concat $ concat xs
21
22
23 -- | Takes a list, and returns True if its elements are pairwise
24 -- equal. Returns False otherwise.
25 all_equal :: (Eq a) => [a] -> Bool
26 all_equal xs =
27 and $ map (== first_element) other_elements
28 where
29 first_element = head xs
30 other_elements = tail xs