1 -- | The Misc module contains helper functions that seem out of place
6 import Data.List (intersect)
7 import Test.Framework (Test, testGroup)
8 import Test.Framework.Providers.HUnit (testCase)
9 import Test.Framework.Providers.QuickCheck2 (testProperty)
11 import Test.QuickCheck
14 -- | The standard factorial function. See
15 -- <http://www.willamette.edu/~fruehr/haskell/evolution.html> for
16 -- possible improvements.
26 factorial :: Int -> Int
29 | n > 20 = error "integer overflow in factorial function"
30 | otherwise = product [1..n]
33 -- | Takes a three-dimensional list, and flattens it into a
34 -- one-dimensional one.
38 -- >>> flatten [ [[1,2], [3,4]], [[5,6], [7,8]] ]
41 flatten :: [[[a]]] -> [a]
42 flatten xs = concat $ concat xs
45 -- | Switch the x and z dimensions of a three-dimensional list.
46 transpose_xz :: [[[a]]] -> [[[a]]]
48 transpose_xz [[]] = [[]]
49 transpose_xz [[[]]] = [[[]]]
51 [[[ m !! x !! y !! z | x <- [0..xsize]]
55 zsize = (length m) - 1
56 ysize = length (head m) - 1
57 xsize = length (head $ head m) - 1
59 -- | Takes a list, and returns True if its elements are pairwise
60 -- equal. Returns False otherwise.
61 all_equal :: (Eq a) => [a] -> Bool
63 all (== first_element) other_elements
65 first_element = head xs
66 other_elements = tail xs
69 -- | Returns 'True' if the lists xs and ys are disjoint, 'False'
74 -- >>> disjoint [1,2,3] [4,5,6]
77 -- >>> disjoint [1,2,3] [3,4,5]
80 disjoint :: (Eq a) => [a] -> [a] -> Bool
86 prop_factorial_greater :: Int -> Property
87 prop_factorial_greater n =
88 n <= 20 ==> factorial n >= n
91 test_flatten1 :: Assertion
93 assertEqual "flatten actually works" expected_list actual_list
95 target = [[[1::Int]], [[2, 3]]]
96 expected_list = [1, 2, 3]
97 actual_list = flatten target
100 misc_tests :: Test.Framework.Test
102 testGroup "Misc Tests" [
103 testCase "flatten (1)" test_flatten1 ]
106 misc_properties :: Test.Framework.Test
108 testGroup "Misc Properties" [
109 testProperty "factorial greater" prop_factorial_greater ]