-
Notifications
You must be signed in to change notification settings - Fork 0
/
Senaste senaste
85 lines (63 loc) · 1.65 KB
/
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
;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