-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFourthAssignment.lsp
105 lines (93 loc) · 2.54 KB
/
FourthAssignment.lsp
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
; Solution to Problem 1
(defun sum (L)
(if (endp L)
0
(let ((x (sum (cdr L))))
(+ x (car L)))))
; Solution to Problem 2
(defun neg-nums (L)
(if (endp L)
nil
(let ((X (neg-nums (cdr L))))
(cond ((minusp (car L)) (cons (car L) X))
(t x)))))
; Solution to Problem 3
(defun inc-list-2 (L N)
(if (endp L)
nil
(let ((X (inc-list-2 (cdr L) N)))
(cons (+ (car L) N) X))))
; Solution to Problem 4
(defun insert (N L)
(if (endp L)
(list N)
(let ((X (insert N (cdr L))))
(cond ((> (car L) N) (cons N L))
(t (cons (car L) X))))))
; Solution to Problem 5
(defun isort (L)
(if (endp L)
nil
(let ((X (isort (cdr L))))
(insert (car L) X))))
; Solution to Problem 6
(defun split-list (L)
(if (endp L)
(list ()())
(let ((X (split-list (cdr L))))
(list (cons (car L) (car (cdr X))) (car X)))))
; Solution to Problem 7
(defun partition (L P)
(if (endp L)
(list ()())
(let ((X (partition (cdr L) P)))
(cond ((< (car L) P) (list (cons(car L)(car X)) (cadr X)))
(t (list (car x)(cons (car L)(cadr X))))))))
; Solution to Problem 8
(defun pos (e l)
(cond ((endp l) 0)
((equal e (car l)) 1)
(t (let ((x (pos e (cdr l))))
(if (zerop x)
0
(+ x 1))))))
; Solution to Problem 9
(defun split-nums (N)
(if (zerop N)
(list (list N) nil)
(let ((x (split-nums (- N 1))))
(if (evenp N)
(list (cons N (car x)) (cadr x))
(list (car x) (cons N (cadr x)))))))
; Solution to Problem 10
(defun set-union (s1 s2)
(if (endp s1)
s2
(let ((x (set-union (cdr s1) s2)))
(if (member (car s1) x)
x
(cons (car s1) x)))))
; Solution to Problem 11
(defun set-remove (a s1)
(if (endp s1)
nil
(let ((x (set-remove a (cdr s1))))
(if (or (member (car s1) x) (eq (car s1) a))
x
(cons (car s1) x)))))
; Solution to Problem 12
(defun set-excl-union (s1 s2)
(if (endp s1)
s2
(let ((x (set-excl-union (cdr s1) s2)))
(if (member (car s1) x)
(set-remove (car s1) x)
(cons (car s1) x)))))
; Solution to Problem 13
(defun singletons (e)
(if (endp e)
nil
(let ((X (singletons (rest e))))
(if (member (first e) (rest e))
(set-remove (first e) X)
(cons (first e) X)))))