I am trying to create a function where it takes a list of letters as parameter and a single letter as a parameter. I want to remove the single letter from the list.
JavaScript
x
(defun extract-all (lett li)
(let ((new-list nil))
(dolist (letter li new-list)
(if (eql lett letter)
(setf new-list (cons nil new-list))
(setf new-list (cons letter new-list))))))
so if I call the function with (extract-all 'n '(i n t e l l))
, I want it to return i t e l l
with the n
removed.
Advertisement
Answer
First of all, you are not removing letters (characters), but rather symbols:
JavaScript
(type-of 'n)
==> SYMBOL
Second, there is a standard function remove
to do just that:
JavaScript
(remove 'n '(i n t e l l))
==> (I T E L L)
Third, if you remove your “then” clause and reverse the result, you will get what you want:
JavaScript
(defun my-remove (object list)
(let ((new-list nil))
(dolist (element list (nreverse new-list))
(unless (eql object element)
(push element new-list)))))
(my-remove 'n '(i n t e l l))
==> (I T E L L)
Note that there are more ways to skin the cat, e.g.,
JavaScript
(defun my-remove (object list)
(loop for element in list
unless (eql object element)
collect element))
However, it is always best to use the library.