-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_hw.hs
More file actions
72 lines (57 loc) · 1.78 KB
/
Copy pathtypes_hw.hs
File metadata and controls
72 lines (57 loc) · 1.78 KB
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
import Data.List
import Data.Char
import Unsafe.Coerce
data Nat = Zero
| Succ Nat
deriving Show
natToInteger :: Nat -> Integer
natToInteger Zero = 0
natToInteger (Succ n) = natToInteger n + 1
natToInteger' :: Nat -> Integer
natToInteger' = \ n -> genericLength [c | c <- show n, c == 'S']
natToInteger'' :: Nat -> Integer
natToInteger'' = head . m
where m Zero = [0]
m (Succ x) = [sum [x| x <- (1:m x)]]
integerToNat :: Integer -> Nat
integerToNat 0 = Zero
integerToNat n = (Succ (integerToNat (n-1)))
add :: Nat -> Nat -> Nat
add Zero n = n
add (Succ m) n = Succ (add m n)
mult :: Nat -> Nat -> Nat
mult m Zero = Zero
mult m (Succ n) = add m (mult m n)
--data Ordering = LT | EQ | GT
data Tree = Leaf Integer | Node Tree Integer Tree
--compare :: (Ord a) => a -> a -> Ordering
occurs :: Integer -> Tree -> Bool
occurs m (Leaf n) = m == n
occurs m (Node l n r) =
case compare m n of
LT -> occurs m l
GT -> occurs m r
EQ -> True
occurs' :: Integer -> Tree -> Bool
occurs' m (Leaf n) = m==n
occurs' m (Node l n r)
| m == n = True
| m < n = occurs' m l
| otherwise = occurs' m r
data LeafTree = LLeaf Integer | LNode LeafTree LeafTree
deriving Show
--a tree is balanced if the number of leaves in the left and right subtree of
--every node differs by at most one, with leaves themselves being trivially balanced
leaves :: LeafTree -> Integer
leaves (LLeaf _) = 1
leaves (LNode l r) = leaves l + leaves r
balanced :: LeafTree -> Bool
balanced (LLeaf _) = True
balanced (LNode l r) = abs(leaves l - leaves r) <= 1 && balanced l && balanced r
halve :: [Integer] -> ([Integer], [Integer])
halve xs = splitAt (length xs `div` 2) xs
balance :: [Integer] -> LeafTree
balance [x] = LLeaf x
balance xs = LNode (balance ys) (balance zs)
where
(ys, zs) = halve xs