-
Notifications
You must be signed in to change notification settings - Fork 10
/
11.hs
90 lines (70 loc) · 2.6 KB
/
11.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{-# LANGUAGE ScopedTypeVariables #-}
import Data.List
import Test.Hspec
import Test.Hspec.QuickCheck
----------------------------------------------------------------
main :: IO ()
main = hspec $ do
describe "my_from_list" $
prop "contains elements in order" $ \(es :: [Int]) ->
prop_ordered (my_from_list es) es
describe "my_member" $ do
prop "behaves like model" prop_member_model
prop "returns True for a memeber" prop_member
prop_ordered :: Tree Int -> [Int] -> Expectation
prop_ordered t es = inorder t `shouldBe` nub (sort es)
inorder :: Tree a -> [a]
inorder Leaf = []
inorder (Node l x r) = inorder l ++ [x] ++ inorder r
prop_member :: [Int] -> Expectation
prop_member [] = return ()
prop_member es = and rs `shouldBe` True
where
t = from_list es
rs = [my_member e t | e <- es]
prop_member_model :: Int -> [Int] -> Expectation
prop_member_model x [] = my_member x Leaf `shouldBe` False
prop_member_model x es = my_member x t `shouldBe` elem x es
where
t = from_list es
-- This code is intentionally duplicated for the test of my_member.
from_list :: Ord a => [a] -> Tree a
from_list es = foldl ins Leaf es
where
ins :: Ord a => Tree a -> a -> Tree a
ins Leaf e = Node Leaf e Leaf
ins (Node l x r) e = case compare e x of
LT -> Node (ins l e) x r
EQ -> Node l e r
GT -> Node l x (ins r e)
----------------------------------------------------------------
data Tree a = Leaf | Node (Tree a) a (Tree a) deriving (Eq,Show)
----------------------------------------------------------------
my_member :: Ord a => a -> Tree a -> Bool
my_member _ Leaf = False
my_member e (Node l x r) = case compare e x of
LT -> undefined
EQ -> undefined
GT -> undefined
----------------------------------------------------------------
my_insert :: Ord a => a -> Tree a -> Tree a
my_insert e Leaf = Node Leaf e Leaf
my_insert e (Node l x r) = case compare e x of
LT -> undefined
EQ -> Node l e r
GT -> undefined
----------------------------------------------------------------
my_from_list :: Ord a => [a] -> Tree a
my_from_list es = foldl ins Leaf es
where
ins t e = my_insert e t
----------------------------------------------------------------
my_show_tree :: Show a => Tree a -> String
my_show_tree t = my_show_tree' t ""
my_show_tree' :: Show a => Tree a -> String -> String
my_show_tree' Leaf _ = ""
my_show_tree' (Node Leaf x Leaf) _ = show x
my_show_tree' (Node l x r) pref =
show x ++ "\n"
++ pref ++ "+" ++ my_show_tree' l (pref ++ " ") ++ "\n"
++ pref ++ "+" ++ my_show_tree' r (pref ++ " ")