-
Notifications
You must be signed in to change notification settings - Fork 0
/
Labb #2 senaste senaste senaste
132 lines (93 loc) · 2.61 KB
/
Labb #2 senaste senaste senaste
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
(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)
((= (car lst1) (car lst2)) (list-equal? (cdr lst1) (cdr lst2)))
(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