-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.idr
79 lines (65 loc) · 2.42 KB
/
views.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
73
74
75
76
77
78
79
import Data.List.Views
import Data.Vect
import Data.Vect.Views
import Data.Nat.Views
-- used for exercises 10.3.4
import datastore_module
import shape_abs
-- 10.1.6
data TakeN : List a -> Type where
Fewer : TakeN xs
Exact : (n_xs : List a) -> TakeN (n_xs ++ rest)
takeN : (n : Nat) -> (xs : List a) -> TakeN xs
takeN Z xs = Exact []
takeN (S k) [] = Fewer
takeN (S k) (x :: xs) with (takeN k xs)
takeN (S k) (x :: xs) | Fewer = Fewer
takeN (S k) (x :: (n_xs ++ rest)) | (Exact n_xs) = Exact (x :: n_xs)
groupByN : (n : Nat) -> (xs : List a) -> List (List a)
groupByN n xs with (takeN n xs)
groupByN n xs | Fewer = [xs]
groupByN n (n_xs ++ rest) | (Exact n_xs) = n_xs :: groupByN n rest
halves : List a -> (List a, List a)
halves xs with (takeN (length xs `div` 2) xs)
halves xs | Fewer = ([], xs)
halves (n_xs ++ rest) | (Exact n_xs) = (n_xs, rest)
-- 10.2.5
total
equalSuffix : Eq a => List a -> List a -> List a
equalSuffix xs ys with (snocList xs)
equalSuffix [] ys | Empty = []
equalSuffix (zs ++ [x]) ys | (Snoc rec) with (snocList ys)
equalSuffix (zs ++ [x]) [] | (Snoc rec) | Empty = []
equalSuffix (zs ++ [x]) (xs ++ [y]) | (Snoc rec) | (Snoc z) =
if x == y then (equalSuffix zs xs | rec | z) ++ [x]
else []
total
mergeSort : Ord a => Vect n a -> Vect n a
mergeSort xs with (splitRec xs)
mergeSort [] | SplitRecNil = []
mergeSort [x] | SplitRecOne = [x]
mergeSort (ys ++ zs) | (SplitRecPair lrec rrec) =
merge (mergeSort ys | lrec) (mergeSort zs | rrec)
total
toBinary : Nat -> String
toBinary k with (halfRec k)
toBinary Z | HalfRecZ = ""
toBinary (n + n) | (HalfRecEven rec) = toBinary n | rec ++ "0"
toBinary (S (n + n)) | (HalfRecOdd rec) = toBinary n | rec ++ "1"
total
palindrome : Eq a => List a -> Bool
palindrome xs with (vList xs)
palindrome [] | VNil = True
palindrome [x] | VOne = True
palindrome (x :: (ys ++ [y])) | (VCons rec) =
if x == y then palindrome ys | rec else False
-- 10.3.4
getValues : DataStore (SString .+. val_schema) -> List (SchemaType val_schema)
getValues input with (storeView input)
getValues input | SNil = []
getValues (addToStore (key, value) store) | (SAdd rec) = value :: getValues store | rec
area : Shape -> Double
area s with (shapeView s)
area (triangle base height) | STriangle = base * height / 2
area (rectangle width height) | SRectangle = width * height
area (circle radius) | SCircle = radius * radius * pi