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
29 | n > 20 = error "integer overflow in factorial function"
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.
63 all_equal :: (Eq a) => [a] -> Bool
65 all (== first_element) other_elements
67 first_element = head xs
68 other_elements = tail xs
71 -- | Returns 'True' if the vectors xs and ys are disjoint, 'False'
76 -- >>> let xs = Data.Vector.fromList [1,2,3]
77 -- >>> let ys = Data.Vector.fromList [4,5,6]
81 -- >>> let ys = Data.Vector.fromList [3,4,5]
85 disjoint :: (Eq a) => V.Vector a -> V.Vector a -> Bool
87 intersect xs ys == V.empty
89 intersect :: (Eq a) => V.Vector a -> V.Vector a -> V.Vector a
91 V.filter (`V.elem` zs) ws
93 prop_factorial_greater :: Int -> Property
94 prop_factorial_greater n =
95 n <= 20 ==> factorial n >= n
98 test_flatten1 :: Assertion
100 assertEqual "flatten actually works" expected_list actual_list
102 target = [[[1::Int]], [[2, 3]]]
103 expected_list = [1, 2, 3]
104 actual_list = flatten target
107 misc_tests :: Test.Framework.Test
109 testGroup "Misc Tests" [
110 testCase "flatten (1)" test_flatten1 ]
113 misc_properties :: Test.Framework.Test
115 testGroup "Misc Properties" [
116 testProperty "factorial greater" prop_factorial_greater ]