]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Misc.hs
Clean up imports/exports.
[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 import qualified Data.Vector as V (Vector, elem, empty, filter)
7 import Test.Framework (Test, testGroup)
8 import Test.Framework.Providers.HUnit (testCase)
9 import Test.Framework.Providers.QuickCheck2 (testProperty)
10 import Test.HUnit (Assertion, assertEqual)
11 import Test.QuickCheck
12
13
14 -- | The standard factorial function. See
15 -- <http://www.willamette.edu/~fruehr/haskell/evolution.html> for
16 -- possible improvements.
17 --
18 -- Examples:
19 --
20 -- >>> factorial 0
21 -- 1
22 --
23 -- >>> factorial 4
24 -- 24
25 --
26 factorial :: Int -> Int
27 factorial n
28 | n <= 1 = 1
29 | n > 20 = error "integer overflow in factorial function"
30 | otherwise = product [1..n]
31
32
33 -- | Takes a three-dimensional list, and flattens it into a
34 -- one-dimensional one.
35 --
36 -- Examples:
37 --
38 -- >>> flatten [ [[1,2], [3,4]], [[5,6], [7,8]] ]
39 -- [1,2,3,4,5,6,7,8]
40 --
41 flatten :: [[[a]]] -> [a]
42 flatten xs = concat $ concat xs
43
44
45 -- | Switch the x and z dimensions of a three-dimensional list.
46 transpose_xz :: [[[a]]] -> [[[a]]]
47 transpose_xz [] = []
48 transpose_xz [[]] = [[]]
49 transpose_xz [[[]]] = [[[]]]
50 transpose_xz m =
51 [[[ m !! x !! y !! z | x <- [0..xsize]]
52 | y <- [0..ysize]]
53 | z <- [0..zsize]]
54 where
55 zsize = (length m) - 1
56 ysize = length (head m) - 1
57 xsize = length (head $ head m) - 1
58
59 -- | Takes a list, and returns True if its elements are pairwise
60 -- equal. Returns False otherwise.
61 all_equal :: (Eq a) => [a] -> Bool
62 all_equal xs =
63 all (== first_element) other_elements
64 where
65 first_element = head xs
66 other_elements = tail xs
67
68
69 -- | Returns 'True' if the vectors xs and ys are disjoint, 'False'
70 -- otherwise.
71 --
72 -- Examples:
73 --
74 -- >>> let xs = Data.Vector.fromList [1,2,3]
75 -- >>> let ys = Data.Vector.fromList [4,5,6]
76 -- >>> disjoint xs ys
77 -- True
78 --
79 -- >>> let ys = Data.Vector.fromList [3,4,5]
80 -- >>> disjoint xs ys
81 -- False
82 --
83 disjoint :: (Eq a) => V.Vector a -> V.Vector a -> Bool
84 disjoint xs ys =
85 intersect xs ys == V.empty
86 where
87 intersect :: (Eq a) => V.Vector a -> V.Vector a -> V.Vector a
88 intersect ws zs =
89 V.filter (`V.elem` zs) ws
90
91 prop_factorial_greater :: Int -> Property
92 prop_factorial_greater n =
93 n <= 20 ==> factorial n >= n
94
95
96 test_flatten1 :: Assertion
97 test_flatten1 =
98 assertEqual "flatten actually works" expected_list actual_list
99 where
100 target = [[[1::Int]], [[2, 3]]]
101 expected_list = [1, 2, 3]
102 actual_list = flatten target
103
104
105 misc_tests :: Test.Framework.Test
106 misc_tests =
107 testGroup "Misc Tests" [
108 testCase "flatten (1)" test_flatten1 ]
109
110
111 misc_properties :: Test.Framework.Test
112 misc_properties =
113 testGroup "Misc Properties" [
114 testProperty "factorial greater" prop_factorial_greater ]