-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter5-3-filter-list.rkt
69 lines (59 loc) · 1.38 KB
/
chapter5-3-filter-list.rkt
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
#lang pie
;; Define a function called filter-list which takes (in addition to the type
;; argument for the list element) one (-> E Nat) argument representing a
;; predicate and one (List E) argument.
;;
;; The function evaluates to a (List E) consisting of elements from the list
;; argument where the predicate is true.
;;
;; Consider the predicate to be false for an element if it evaluates to zero,
;; and true otherwise.
(claim step-filter
(Pi ((E U))
(-> (-> E Nat) E (List E) (List E) (List E))))
(define step-filter
(lambda (E)
(lambda (pred h _ filtered)
(rec-Nat (pred h)
filtered
(lambda (n-1 _)
(:: h filtered))))))
(claim filter-list
(Pi ((E U))
(-> (-> E Nat) (List E) (List E))))
(define filter-list
(lambda (E)
(lambda (pred es)
(rec-List es
(the (List E) nil)
(step-filter E pred)))))
(claim not
(-> Nat Nat))
(define not
(lambda (n)
(rec-Nat n
1
(lambda (n-1 notzero)
0))))
(claim id
(-> Nat Nat))
(define id
(lambda (n) n))
(check-same (List Nat)
(filter-list
Nat
id
(:: 1 (:: 2 (:: 3 nil))))
(:: 1 (:: 2 (:: 3 nil))))
(check-same (List Nat)
(filter-list
Nat
id
(:: 1 (:: 0 (:: 2 (:: 3 nil)))))
(:: 1 (:: 2 (:: 3 nil))))
(check-same (List Nat)
(filter-list
Nat
(lambda (n) (not (id n)))
(:: 1 (:: 0 (:: 2 (:: 3 nil)))))
(:: 0 nil))