-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions
More file actions
152 lines (113 loc) · 5.32 KB
/
functions
File metadata and controls
152 lines (113 loc) · 5.32 KB
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
; first exercise
(defun divisibleBy (x l)
(if (= x 0)
(return-from divisibleBy l)) ; if x = o then return the input
(setq laux '())
(doList (n l) ; loop input list
(if (= n 0) ; if element = 0 then element belongs to result list
(setq laux (cons n laux) )
(if (= 0 (mod x n)) ; if x module n = 0, element belongs to result list
(setq laux (cons n laux) )
(if (= 0 (mod n x)) ; if n module x = 0, element belongs to result list
(setq laux (cons n laux) )))
)
)
(return-from divisibleBy (reverse laux)) ; reverse result
)
(print (divisibleBy 10 '(4 30 11 5 0)))
(print (divisibleBy 6 '(2 6 1 9 18) ))
; second exercise
; get elements from list and sublits and return a new list with all the elements
; for example: '(a (b c) (c b) a) => '(a b c c b a)
(defun getListPlain (l1 l2)
(doList (n l1)
(if (listp n)
(setq l2 (getListPlain n l2))
(setq l2 (cons n l2))
)
)
(return-from getListPlain l2)
)
; get reverse list to validate symmetrical structure
(defun rev (l)
(cond
((null l) '())
(T
(if (listp (car l)) ; if the element is a list, we call rev recursively to reverse the sublist
(append (rev (cdr l)) (list (rev (car l))))
(append (rev (cdr l)) (list (car l))))))) ; if not the element is returned
(defun isPalindrome (l)
(setq laux (getListPlain l '())) ; call getListPlain to get the plain list
(and (equal laux (reverse laux)) (equal (rev l) l)); compare list and its reverse. it also compares if the structue is symmetrical
)
(print (isPalindrome '(a b a) ))
(print (isPalindrome '(a (b c) (c b) a)))
(print (isPalindrome '(a (b c) ((c) b) a)))
(print (isPalindrome '(a (b (c d)) ((c d) b) a ) ))
(print (isPalindrome '( a (b () b) a)))
(print (isPalindrome '((a b) a (b a)) ))
; third exercise
(defun reverse-aux (x)
(if (eq x '())
'()
(append (reverse-aux (rest x)) (list (car x)))))
(defvar l1 '(1 2 3 (5 6) ((7 (8)))))
(format t "original list: ~A~%" l1)
(format t "revert: ~A~%" (reverse-aux l1)) ; (((7 (8))) (5 6) 3 2 1)
(defvar l2 '())
(format t "original list: ~A~%" l2)
(format t "revert: ~A~%" (reverse-aux l2)) ; NIL
(defvar l3 '(1 2 3 (6) (9) 34 5))
(format t "original list: ~A~%" l3)
(format t "revert: ~A~%" (reverse-aux l3)) ; (5 34 (9) (6) 3 2 1)
; fourth exercise
(defun search-duplicates(l)
(maplist #'(lambda (y) ; travel through list wit maplist
(if (member (car y) (cdr y) :test #'equal) ; if the first element is in the rest of the list
; EQUAL (if you just care about elements)
(return-from search-duplicates '(T)) 1)) l) ; return '(T) otherwise continue travelling
)
(defun duplicate-entries(l)
(let (result) (if (eq T (first (funcall #'search-duplicates l))) T nil)))
(defvar l1 '((a) (c) (b)))
(format t "original list: ~A~%" l1)
(format t "result: ~A~%" (duplicate-entries l1)) ; NIL
(defvar l2 '(1 (1 2 (4) (1 2 3)) (1 2 (5) (1 2 3)) 1))
(format t "original list: ~A~%" l2)
(format t "result: ~A~%" (duplicate-entries l2)) ; T
(defvar l3 '((a) (2) (b) (z (2))))
(format t "original list: ~A~%" l3)
(format t "result: ~A~%" (duplicate-entries l3)) ; NIL
(defvar l4 '((a) (c) (b) (z (c)) (c)))
(format t "original list: ~A~%" l4)
(format t "result: ~A~%" (duplicate-entries l4)) ; T
(defvar l5 '(a b (a) c d)) ; ’(a b (a) c d)
(format t "original list: ~A~%" l5)
(format t "result: ~A~%" (duplicate-entries l5)) ; NIL
(defvar l6 '((a b) b c (a b)))
(format t "original list: ~A~%" l6)
(format t "result: ~A~%" (duplicate-entries l6)) ; T
; fifth exercise
(defun has-number-p (exp)
(if (numberp exp) ; it is a number
(return-from has-number-p T)
(if (listp exp) ; it is a lits
(let ((result nil))
(dolist (el exp result) ; travell through expression
(if (listp el) ; if the first element in the list is a list
(if (eq (has-number-p el) T) (return-from has-number-p T)) ; calll recursively
(if (some #'numberp (list el)) (return-from has-number-p T))))) ; if el is a number return T
(return-from has-number-p nil)))) ; it is not a number or a list
(format t "result has a number 1: ~A~%" (has-number-p '(b c d (3)))) ; Return T (because 3 is in the list)
(format t "result has a number 2: ~A~%" (has-number-p '(a (b 2) c))) ; Returns T (because 2 is in the list)
(format t "result has a number 3: ~A~%" (has-number-p '(a (b) c))) ; Returns NIL (no numbers)
(format t "result has a number 4: ~A~%" (has-number-p '(a (b 3)))) ; Returns T (because 3 is in the sublist)
(format t "result has a number 5: ~A~%" (has-number-p 5)) ; Returns T (because 5 is a number)
(format t "result has a number 6: ~A~%" (has-number-p 'hello)) ; Returns NIL (no numbers)
; Another function more efficient for same problem given by ChatGPT
(defun has-number-p2 (exp)
(cond
((numberp exp) T) ; Base case: if exp is a number, return T
((listp exp) ; If exp is a list, check its elements
(some #'has-number-p2 exp))
(t nil))) ; If exp is neither a number nor a list, return nil