-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab4.rkt
162 lines (135 loc) · 3.37 KB
/
lab4.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#lang racket
(define (map function list)
(if (null? list)
'()
(cons (function (car list)) (map function (cdr list)))
)
)
(map (lambda (x) (+ x 1)) '(1 2 3 4))
(define (filter predicate list)
(if (null? list)
'()
(if (predicate (car list))
(cons (car list) (filter predicate (cdr list)))
(filter predicate (cdr list))
)
)
)
(filter (lambda (x) (if (= (remainder x 2) 0) #t #f)) '(1 2 3 4))
(define (generate-interval start end)
(if (> start end)
'()
(cons start (generate-interval (+ start 1) end))
)
)
(generate-interval 3 6)
(define (zip list1 list2)
(if (or (null? list1) (null? list2))
'()
(cons (cons (car list1) (car list2)) (zip (cdr list1) (cdr list2)))
)
)
(zip '(1 2 3) '(4 5 5 6))
(define (member-deep? list element)
(if (null? list)
#f
(if (equal? (car list) element)
#t
(or
(if (list? (car list))
(member-deep? (car list) element)
#f)
(member-deep? (cdr list) element)
)
)
)
)
(member-deep? '((1 2) 3 4) 5)
(define (my-flatten list)
(if (null? list)
'()
(if (pair? (car list))
(if (> (length (car list)) 1)
(my-flatten (cons (car (car list)) (cons (cdr (car list)) (cdr list))))
(my-flatten (cons (car (car list)) (cdr list)))
)
(cons (car list) (my-flatten (cdr list)))
)
)
)
(my-flatten '((1 2) (1 2) 3 (1 2 (3 4))))
(define (fold f nv xs)
(if (null? xs)
nv
(f (car xs) (fold f nv (cdr xs)))
)
)
(define (insertion-sort list)
(define (insert-element element sorted-list)
(if (null? sorted-list)
(cons element '())
(if (> element (car sorted-list))
(cons (car sorted-list) (insert-element element (cdr sorted-list) ))
(cons element sorted-list)
)
)
)
(fold insert-element '() list)
)
(insertion-sort '(2 5 1 6 2 4))
(define (intersect list1 list2)
(define (contains? elem list)
(if (null? list)
#f
(if (= (car list) elem)
#t
(contains? elem (cdr list))
)
)
)
(if (null? list1)
'()
(if (contains? (car list1) list2)
(cons (car list1) (intersect (cdr list1) list2))
(intersect (cdr list1) list2)
)
)
)
(intersect '(1 2 3) '(4 3 5 2))
(define (union list1 list2)
(define (remove-repetitions list)
(fold (lambda (elem list) (if (member elem list) list (cons elem list))) '() list)
)
(remove-repetitions (my-flatten (cons list1 list2)))
)
(union '(1 2 4 6) '(2 5 7 8))
(define (my-map f list)
(if (null? list)
'()
(cons (f (car list)) (my-map f (cdr list)))
)
)
(define (my-filter pred list)
(if (null? list)
'()
(if (pred (car list))
(cons (car list) (my-filter pred (cdr list)))
(cdr list)
)
)
)
(define (fold2 f nv list)
(if (null? list)
nv
(f (car list) (fold2 f nv (cdr list)))
)
)
(define (my-map-fold f list)
(fold2 f '() list)
)
(define (compose function-list)
(fold2 (lambda (func func-list) (lambda (x) (func x))) '() function-list))
((compose (list
(lambda (x) (+ x 1))
(lambda (x) (+ x 2))
(lambda (x) (+ x 3)))) 1)