-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch4.idr
72 lines (54 loc) · 2.17 KB
/
ch4.idr
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.Fin
import Data.Vect
-- 4.1.5
-- a binary search tree
data BSTree : (a : Type) -> Type where
Empty : Ord a => BSTree a
Node : Ord a => (left : BSTree a) -> (val: a) -> (right : BSTree a) -> BSTree a
insert : elem -> BSTree elem -> BSTree elem
insert x Empty = Node Empty x Empty
insert x orig@(Node left val right) = case compare x val of
LT => Node (insert x left) val right
EQ => orig
GT => Node left val (insert x right)
listToTree : Ord a => List a -> BSTree a
listToTree = foldl (\tree, x => insert x tree) Empty
treeToList : Ord a => BSTree a -> List a
treeToList Empty = []
treeToList (Node left val right) = treeToList left ++ [val] ++ treeToList right
data Expr = Val Integer | Add Expr Expr | Sub Expr Expr | Mul Expr Expr
evaluate : Expr -> Integer
evaluate (Val x) = x
evaluate (Add x y) = evaluate x + evaluate y
evaluate (Sub x y) = evaluate x - evaluate y
evaluate (Mul x y) = evaluate x * evaluate y
maxMaybe : Ord a => Maybe a -> Maybe a -> Maybe a
maxMaybe Nothing Nothing = Nothing
maxMaybe (Just x) Nothing = Just x
maxMaybe Nothing (Just y) = Just y
maxMaybe (Just x) (Just y) = Just (max x y)
data Shape = Triangle Double Double
| Rectangle Double Double
| Circle Double
area : Shape -> Double
area (Triangle base height) = 0.5 * base * height
area (Rectangle length height) = length * height
area (Circle radius) = pi * radius * radius
data Picture = Primitive Shape
| Combine Picture Picture
| Rotate Double Picture
| Translate Double Double Picture
biggestTriangle : Picture -> Maybe Double
biggestTriangle (Primitive t@(Triangle _ _)) = Just (area t)
biggestTriangle (Primitive _) = Nothing
biggestTriangle (Combine p1 p2) = maxMaybe (biggestTriangle p1) (biggestTriangle p2)
biggestTriangle (Rotate _ p) = biggestTriangle p
biggestTriangle (Translate _ _ p) = biggestTriangle p
-- selected from 4.2.4
take : (k : Fin (S n)) -> Vect n a -> Vect (finToNat k) a
take FZ _ = []
take (FS p) (x :: xs) = x :: take p xs
sumEntries : Num a => {n : Nat} -> Integer -> Vect n a -> Vect n a -> Maybe a
sumEntries {n} k xs ys = do
i <- integerToFin k n
Just $ index i xs + index i ys