]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Misc.hs
Fix some more hLint warnings.
[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 --
11 -- Examples:
12 --
13 -- >>> factorial 0
14 -- 1
15 --
16 -- >>> factorial 4
17 -- 24
18 --
19 factorial :: Int -> Int
20 factorial n
21 | n <= 1 = 1
22 | n > 20 = error "integer overflow in factorial function"
23 | otherwise = product [1..n]
24
25
26 -- | Takes a three-dimensional list, and flattens it into a
27 -- one-dimensional one.
28 --
29 -- Examples:
30 --
31 -- >>> flatten [ [[1,2], [3,4]], [[5,6], [7,8]] ]
32 -- [1,2,3,4,5,6,7,8]
33 --
34 flatten :: [[[a]]] -> [a]
35 flatten xs = concat $ concat xs
36
37
38 -- | Switch the x and z dimensions of a three-dimensional list.
39 transpose_xz :: [[[a]]] -> [[[a]]]
40 transpose_xz [] = []
41 transpose_xz [[]] = [[]]
42 transpose_xz [[[]]] = [[[]]]
43 transpose_xz m =
44 [[[ m !! x !! y !! z | x <- [0..xsize]]
45 | y <- [0..ysize]]
46 | z <- [0..zsize]]
47 where
48 zsize = (length m) - 1
49 ysize = length (head m) - 1
50 xsize = (length $ head $ head m) - 1
51
52 -- | Takes a list, and returns True if its elements are pairwise
53 -- equal. Returns False otherwise.
54 all_equal :: (Eq a) => [a] -> Bool
55 all_equal xs =
56 all (== first_element) other_elements
57 where
58 first_element = head xs
59 other_elements = tail xs