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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
| First, Rest
(car list)
first element
(equal
(car '("a" "b" "c"))
"a"
)
(cdr list)
rest elements
(equal
(cdr '(0 1 2 3 4))
'(1 2 3 4))
Nth, Nth Rest, Not N Rest
(nth n list)
nth element. (Index starts from 0.)
(equal
(nth 1 '(0 1 2 3 4))
1)
(nthcdr n list)
rest starting at n.
(equal
(nthcdr 2 '(0 1 2 3 4))
'(2 3 4))
(butlast list n)
without the last n elements.
(equal
(butlast '(0 1 2 3 4 5))
'(0 1 2 3 4))
(equal
(butlast '(0 1 2 3 4 5) 2)
'(0 1 2 3))
Last, Last N
(last list)
last as a list. i.e. return (cons lastElement nil).
To get the actual last item of a list, do (car (last list))
(equal
(car (last (list "a" "b" "c")))
"c"
)
(equal
(last (list "a" "b" "c"))
(cons "c" nil)
)
(last list &optional n)
last n items.
(equal
(last '(0 1 2 3 4 5 6) 2)
'(5 6)
)
Add Element
(push new listVar)
Add element to the front.
Modify the listVar.
Return the new value of listVar
(setq xx '(1))
;; after push, the var is modified, and the var's new value is returned
(eq (push 2 xx) xx)
(equal xx '(2 1))
The variable is modified even if the pushed list is inside a list.
;; a list of lists
(setq xx '((1 2) (3 4) (5 6)))
;; push b to one of the list
(equal
(push "b" (nth 1 xx))
'("b" 3 4))
;; the xx is modified
(equal
xx
'((1 2) ("b" 3 4) (5 6)))
also if the variable is a vector:
;; a vector of lists
(setq xx [(1 2) (3 4) (5 6)])
;; push b to one of the list
(equal
(push "b" (aref xx 1))
'("b" 3 4 ))
;; the xx is modified
(equal
xx
[(1 2) ("b" 3 4 ) (5 6)]
)
(add-to-list listVar ELEMENT &optional APPEND COMPARE-FN)
Add to list when not already in it.
(setq xx '(1 2 3))
;; add "a" to it. return the modified var
(eq
(add-to-list 'xx "a" )
xx)
;; check the new value
(equal
xx
'("a" 1 2 3))
(add-to-ordered-list listVar ELEMENT &optional ORDER)
Add to specific position in list, if it does not exist. The list is reordered.
Remove Element
(pop listVar)
Remove first element from the variable. Return the removed element.
(setq xx '(1 2 3 4))
(equal (pop xx) 1)
(equal xx '(2 3 4))
(nbutlast listVar n)
Remove last n elements from the variable. Return the new value of the variable.
(setq xx '(0 1 2 3))
(equal (nbutlast xx 1) '(0 1 2))
(equal xx '(0 1 2))
Replace Element
(setcar listVar new)
Replace the first element in listVar with new. Return new.
(setq xx '(1 2 3 4))
(equal (setcar xx "a") "a")
(equal xx '("a" 2 3 4))
(setcdr listVar newTail)
Replace the rest of elements in listVar with newTail. Return newTail.
Warning: if you want the result to be a Proper List, the newTail should be a proper list.
(setq xx '(1 2 3 4))
(equal
(setcdr xx (cons "a" nil))
(cons "a" nil))
(equal xx '(1 "a"))
(member x list)
Check if x is in list. If so, return a list starting with the first occurrence of object. Else return nil.
Comparison done using equal. [see Emacs Lisp: Test Equality]
(member "4" '("3" "4" "5")) ;; ("4" "5")
(member-ignore-case x list)
same as member, except that x should be a string, and comparison ignores letter-case.
(member-ignore-case "A" '("b" "a")) ; ("a")
(memq x list)
Same as member, but comparison done using eq. Use this if all items are Symbols.
(memql x list)
Same as member, but comparison done using eql.
remq
(remq x list)
Remove all x in list.
Returns a new list.
The original list is unchanged.
Comparison is done with eq.
(setq xx '(3 4 5))
(remq 4 xx) ; (3 5)
xx ; (3 4 5)
delq
(delq x list)
Remove all x in list.
The original list is destroyed.
Returns a new list.
Comparison is done with eq.
(setq xx '(3 4 5))
;; always set result to the same var
(setq xx (delq 4 xx)) ; (3 5)
Delete Duplicates
delete-dups
(delete-dups list)
This function destructively removes all duplicates from list, return a new list.
The first one is kept among duplicates. Comparison done using equal.
(setq xx '(3 4 5 3 2))
(setq xx (delete-dups xx)) ; (3 4 5 2)
Backquote Reader Macro is a lisp reader macro , which transforms syntax before lisp compiler runs the code.
Backquote Reader Macro is useful for:
Eval some of the list element only.
Change list into just its elements, inside a list.
Eval Some Elements Only
Syntax:
`(x1 x2 rest)
for each element you want to eval, put a comma in front of it. e.g. eval second elemnt
`(x1 ,x2 x3)
;; example of eval only some element of a list
(setq x1 1)
(setq x2 2)
;; eval just x1
(setq x3 `(0 ,x1 x2))
(equal
x3
'(0 1 x2))
;; true
(proper-list-p x3)
;; true
Change List into Its Elements
Syntax:
`(x1 x2 rest)
for each element that is a list you want to become elements,
put “comma at” ,@ in front of it. e.g. spill x2 as elements:
`(x1 ,@x2 x3)
;; example of turning a list into elements, inside a list
(setq x1 (list 1 2))
(setq x2 (list 3 4))
;; we want the elements of x1 and x2 inside a list, but not as sublist
(setq x3 `(0 ,@ x1 ,@ x2))
(equal
x3
'(0 1 2 3 4))
;; true
(proper-list-p x3)
;; true
;; Backquote Reader Macro can be used to feed a list into a function as args
;; our test function
(defun ff (x y z)
"return args as one string"
(format "%s %s %s" x y z))
;; normal usage
(equal
(ff 2 3 4)
"2 3 4")
;; define a list
(setq xx '(2 3 4))
;; use backquote reader macro, to turn a list into elements, then use eval
(equal
(eval `(ff ,@ xx))
"2 3 4")
|