-
Notifications
You must be signed in to change notification settings - Fork 0
/
Labb #2 16 feb
202 lines (150 loc) · 5.22 KB
/
Labb #2 16 feb
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
(load "music-db.scm")
(load "table-helper.scm")
(load "quicksort-skel.scm")
;Problem 1
(define (atom? n)
(if
(or (null? n) (pair? n) )
#f
#t))
;Problem 2
(define (count-list lst)
(if (null? lst)
0
(+ 1 (count-list(cdr lst)))))
;Problem 3
(define (keep-if pred lat)
(cond
((null? lat) '())
((pred (car lat))
(cons (car lat) (keep-if pred (cdr lat))))
(else (keep-if pred (cdr lat)))))
;Problem 4
(define (first-n numb-of-ele lst)
(cond
((= numb-of-ele 0) '())
((<= (count-list lst) numb-of-ele) lst)
(else (cons (car lst) (first-n (- numb-of-ele 1) (cdr lst))))))
;Problem 5
(define (range from to step)
(if (> from to)
'()
(cons from (range (+ from step) to step))))
;Problem 6
;Iterative
(define (reverse-order lst)
(define (reverse-iter lst fantasyswing)
(if (null? lst)
fantasyswing
(reverse-iter (cdr lst) (cons (car lst) fantasyswing))))
(reverse-iter lst '()))
;Linear Recursive
(define (reverse-order-2 lst)
(if (null? lst)
lst
(append (reverse-order-2 (cdr lst)) (cons (car lst) ()))))
(define (my-append lst1 lst2)
(append (lst1 lst2)))
;Problem 7
(define (map-to-each funktion lst)
(if (null? lst)
'()
(cons (funktion (car lst)) (map-to-each funktion (cdr lst)))))
;Problem 8
(define (count-all lst)
(cond
((null? lst) 0)
((list? (car lst)) (+ (count-all (car lst)) (count-all (cdr lst))))
(else (+ 1 (count-all(cdr lst))))))
(define (keep-if-all pred lat)
(cond
((if (atom? lat) '()
((list? (car lat)) (keep-if-all pred (car lat)))))
((pred (car lat))
(cons (car lat) (keep-if-all pred (cdr lat))))
(else (keep-if-all pred (cdr lat)))))
;Problem 9
;Funkar okej så länge det inte är mer än 1 lista inuti första listan.
;"(define (list-equal? lst1 lst2)
; (cond
; ((and (null? lst1) (null? lst2)) #t)
; ((eq? lst1 lst2) #t)
; ((and (list? (car lst1)) (list? (car lst2))) (if (= (car (car lst1)) (car (car lst2)))
; (list-equal? (cdr (car lst1)) (cdr (car lst2))) #f))
; ((list? (car lst1)) #f)
; ((list? (car lst2)) #f)
; ((eq? (car lst1) (car lst2)) (list-equal? (cdr lst1) (cdr lst2)))
; (else #f)))
(define (list-equal? list1 list2)
(cond
((and (null? list1) (not (null? list2))) #f)
((and (null? list2) (not (null? list1))) #f)
((and (null? list1) (null? list2))#t)
((and (atom? list1) (atom? list2))
(eq? list1 list2))
((and (and (atom? (car list1)) (atom? (car list2))) (eq? (car list1) (car list2))) (list-equal? (cdr list1) (cdr list2)))
((and (list? (car list1)) (list? (car list2)))
(and (list-equal? (car list1) (car list2))
(list-equal? (cdr list1) (cdr list2))))
(else #f)))
;Problem 10
(define (occurs*? val lst)
(if (null? lst)
#f
(cond
((atom? lst) (eq? val lst))
(else (or
(occurs*? val (car lst))
(occurs*? val (cdr lst)))))))
;Problem 11
(define (subst-all gammal ny lst)
(if (null? lst)
'()
(cons (if (eq? gammal (car lst))
ny
(car lst))
(subst-all gammal ny (cdr lst)))))
;Problem 12
(define (partition pivot lon)
(define (phelper pivot lon less-or-equal greater)
(cond
((null? lon) (cons less-or-equal greater))
(( <= (car lon) pivot) (phelper pivot (cdr lon) (cons (car lon) less-or-equal) greater))
(else (phelper pivot (cdr lon) less-or-equal (cons (car lon) greater)))))
(phelper pivot lon '() '()))
;Problem 13
(define (get-first-record lst)
(car lst))
(define (get-rest-records lst)
(cdr lst))
(define (get-title lst)
(car lst))
(define (get-artist lst)
(car (cdr lst)))
(define (get-year lst)
(car (cdr (cdr lst))))
(define (get-genre lst)
(car (cdr (cdr (cdr lst)))))
(define (*the-empty-database*)
'())
(define (add-record record database)
(cons record database))
(define (empty-database? database)
(eq? database '()))
;Problem 14
(define (get-all field testcase database)
(if (empty-database? database)
'()
(cond
((eq? field 'title) (if (list-equal? (get-title (get-first-record database)) testcase)
(add-record (get-first-record database) (get-all field testcase (get-rest-records database)))
(get-all field testcase (get-rest-records database))))
((eq? field 'artist) (if (list-equal? (get-artist (get-first-record database)) testcase)
(add-record (get-first-record database) (get-all field testcase (get-rest-records database)))
(get-all field testcase (get-rest-records database))))
((eq? field 'year) (if (eqv? (get-year (get-first-record database)) testcase)
(add-record (get-first-record database) (get-all field testcase (get-rest-records database)))
(get-all field testcase (get-rest-records database))))
((eq? field 'genre) (if (list-equal? (get-genre (get-first-record database)) testcase)
(add-record (get-first-record database) (get-all field testcase (get-rest-records database)))
(get-all field testcase (get-rest-records database)))))))