1 {-# LANGUAGE BangPatterns #-}
3 -- | The Misc module contains helper functions that seem out of place
16 import qualified Data.Vector as V ( Vector, elem, empty, filter )
17 import Test.Framework ( Test, testGroup )
18 import Test.Framework.Providers.HUnit ( testCase )
19 import Test.Framework.Providers.QuickCheck2 ( testProperty )
20 import Test.HUnit ( Assertion, assertEqual )
21 import Test.QuickCheck ( Property, (==>) )
24 -- | The standard factorial function. See
25 -- <http://www.willamette.edu/~fruehr/haskell/evolution.html> for
26 -- possible improvements.
36 factorial :: Int -> Int
42 | otherwise = go (acc * i) (i - 1)
44 -- | Takes a three-dimensional list, and flattens it into a
45 -- one-dimensional one.
49 -- >>> flatten [ [[1,2], [3,4]], [[5,6], [7,8]] ]
52 flatten :: [[[a]]] -> [a]
53 flatten xs = concat $ concat xs
56 -- | Switch the x and z dimensions of a three-dimensional list.
57 transpose_xz :: [[[a]]] -> [[[a]]]
59 transpose_xz [[]] = [[]]
60 transpose_xz [[[]]] = [[[]]]
62 [[[ m !! x !! y !! z | x <- [0..xsize]]
66 zsize = (length m) - 1
67 ysize = length (head m) - 1
68 xsize = length (head $ head m) - 1
70 -- | Takes a list, and returns True if its elements are pairwise
71 -- equal. Returns False otherwise.
73 -- Only used in tests.
75 all_equal :: (Eq a) => [a] -> Bool
76 all_equal [] = True -- Vacuously
77 all_equal (x:xs) = all (== x) xs
81 -- | Returns 'True' if the vectors xs and ys are disjoint, 'False'
86 -- >>> let xs = Data.Vector.fromList [1,2,3]
87 -- >>> let ys = Data.Vector.fromList [4,5,6]
91 -- >>> let ys = Data.Vector.fromList [3,4,5]
95 -- Only used in tests.
97 disjoint :: (Eq a) => V.Vector a -> V.Vector a -> Bool
99 intersect xs ys == V.empty
101 intersect :: (Eq a) => V.Vector a -> V.Vector a -> V.Vector a
103 V.filter (`V.elem` zs) ws
105 prop_factorial_greater :: Int -> Property
106 prop_factorial_greater n =
107 n <= 20 ==> factorial n >= n
110 test_flatten1 :: Assertion
112 assertEqual "flatten actually works" expected_list actual_list
114 target = [[[1::Int]], [[2, 3]]]
115 expected_list = [1, 2, 3]
116 actual_list = flatten target
119 misc_tests :: Test.Framework.Test
121 testGroup "Misc Tests" [
122 testCase "flatten (1)" test_flatten1 ]
125 misc_properties :: Test.Framework.Test
127 testGroup "Misc Properties" [
128 testProperty "factorial greater" prop_factorial_greater ]