1 {-# LANGUAGE BangPatterns #-}
2 -- | The Misc module contains helper functions that seem out of place
7 import qualified Data.Vector as V (Vector, elem, empty, filter)
8 import Test.Framework (Test, testGroup)
9 import Test.Framework.Providers.HUnit (testCase)
10 import Test.Framework.Providers.QuickCheck2 (testProperty)
11 import Test.HUnit (Assertion, assertEqual)
12 import Test.QuickCheck
15 -- | The standard factorial function. See
16 -- <http://www.willamette.edu/~fruehr/haskell/evolution.html> for
17 -- possible improvements.
27 factorial :: Int -> Int
33 | otherwise = go (acc * i) (i - 1)
35 -- | Takes a three-dimensional list, and flattens it into a
36 -- one-dimensional one.
40 -- >>> flatten [ [[1,2], [3,4]], [[5,6], [7,8]] ]
43 flatten :: [[[a]]] -> [a]
44 flatten xs = concat $ concat xs
47 -- | Switch the x and z dimensions of a three-dimensional list.
48 transpose_xz :: [[[a]]] -> [[[a]]]
50 transpose_xz [[]] = [[]]
51 transpose_xz [[[]]] = [[[]]]
53 [[[ m !! x !! y !! z | x <- [0..xsize]]
57 zsize = (length m) - 1
58 ysize = length (head m) - 1
59 xsize = length (head $ head m) - 1
61 -- | Takes a list, and returns True if its elements are pairwise
62 -- equal. Returns False otherwise.
64 -- Only used in tests.
66 all_equal :: (Eq a) => [a] -> Bool
68 all (== first_element) other_elements
70 first_element = head xs
71 other_elements = tail xs
74 -- | Returns 'True' if the vectors xs and ys are disjoint, 'False'
79 -- >>> let xs = Data.Vector.fromList [1,2,3]
80 -- >>> let ys = Data.Vector.fromList [4,5,6]
84 -- >>> let ys = Data.Vector.fromList [3,4,5]
88 -- Only used in tests.
90 disjoint :: (Eq a) => V.Vector a -> V.Vector a -> Bool
92 intersect xs ys == V.empty
94 intersect :: (Eq a) => V.Vector a -> V.Vector a -> V.Vector a
96 V.filter (`V.elem` zs) ws
98 prop_factorial_greater :: Int -> Property
99 prop_factorial_greater n =
100 n <= 20 ==> factorial n >= n
103 test_flatten1 :: Assertion
105 assertEqual "flatten actually works" expected_list actual_list
107 target = [[[1::Int]], [[2, 3]]]
108 expected_list = [1, 2, 3]
109 actual_list = flatten target
112 misc_tests :: Test.Framework.Test
114 testGroup "Misc Tests" [
115 testCase "flatten (1)" test_flatten1 ]
118 misc_properties :: Test.Framework.Test
120 testGroup "Misc Properties" [
121 testProperty "factorial greater" prop_factorial_greater ]