-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice_programs.lisp
169 lines (106 loc) · 3.16 KB
/
practice_programs.lisp
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
;;returns 0 in case the num is not prime and 1 otherwise
(defun IsPrime(num)
(if (<= num 1)
(setq result 0)
(progn
(if (= num 2)
(setq result 1)
(progn
(setq i 2)
(
loop while (< i num)
do
(if (= (mod num i) 0)
(return 0))
(setf i (+ 1 i))
(return 1)
)
);;end of inner progn
);;end of inner if
);;end of progn
);;end of if
)
;;----------------------------------------------------
;;recursively iterates through the list by breaking it into sublists
;;car and cdr are used to recursively break down the list into smaller lists
;;base condition for recursion is when its nil or has one element(checked by atom keyword)
;;if one element is left in recurision, we check if its prime using IsPrime function
;;incase its prime we increment counter using the + operator
(defun OccurencesOfPrimes(mylist)
(cond ((null mylist) 0)
((atom mylist) (IsPrime mylist))
(t (+ (OccurencesOfPrimes (car mylist))
(OccurencesOfPrimes (cdr mylist)))
)
)
)
;;----------------------------------------------------
;;temporary/intermediate function used inside checkprimes
;if given number is not prime it returns nil else returns the number itself(to be added later to the result)
(defun IsPrime2(num)
(if (<= num 1)
(setq result nil)
(progn
(if (= num 2)
(setq result 1)
(progn
(setq i 2)
(
loop while (< i num)
do
(if (= (mod num i) 0)
(return nil))
(setf i (+ 1 i))
(return num)
)
);;end of inner progn
);;end of inner if
);;end of progn
);;end of if
)
;;----------------------------------------------------
;;recursively breaks down the list and checks if the number is prime or not.
;;incase its prime it gets added to the final result
(defun checkprimes(mylist)
(cond ((null mylist) nil)
((atom mylist) (list (IsPrime2 mylist)))
(t
(append (GetListOfPrimes (car mylist))
(GetListOfPrimes (cdr mylist))
) ) ))
;;----------------------------------------------------
;;calls checkprimes
(defun GetListOfPrimes(mylist)
(remove-duplicates (remove nil (checkprimes mylist)))
)
;;----------------------------------------------------
;;flattens a nested list using recurisive startegy
;;car and cdr are used to recursively break down the list into smaller list
;;base condition for recursion is when its nil or has one element(checked by atom keyword)
;;when base condition is reached it backtracks and append statement is used to create one final list
(defun flatten_list(mylist)
(cond ((null mylist) nil)
((atom mylist) (list mylist))
(t
(append (flatten_list (car mylist))
(flatten_list (cdr mylist))
) ) )
)
;;----------------------------------------------------
;;Function checks if an element is in a given list or not.
;;return 0 if not and the element if its present
(defun checkelement (mylist1 element)
(if (equalp (find element (flatten_list mylist1)) nil)
0
element
);;end of if
)
;;----------------------------------------------------
;;Main Function that gets called.
;;calls checkelement inside
(defun SumIfNot(mylist1 mylist2)
(cond ((null mylist2) 0)
((atom mylist2) (checkelement mylist1 mylist2))
(t (+ (SumIfNot mylist1 (car mylist2))
(SumIfNot mylist1 (cdr mylist2)))
)))