error handling - How to remove an element from a list without using remove function in LISP -
(defun my_remove(e list1 list2) (if (null list1) nil ((setf x car(list1)) (if (!= x e) ((my_append e list2 ) (setf y cdr(list1)) (my_remove(e y list2))) ((setf y cdr(list1)) (my_remove(e y list2)) )))))
i trying write function remove element list getting error "it should lambada function" , don't know code correct or wrong.
problems code
there few problems code. first, let's @ standard indentation
(defun my_remove(e list1 list2) (if (null list1) nil ((setf x car(list1)) ; (i) (if (!= x e) ((my_append e list2 ) ; (ii) (setf y cdr(list1)) ; (iii) (my_remove(e y list2))) ; (iv) ((setf y cdr(list1)) ; (v) (my_remove(e y list2))))))) ; (vi)
each of marked lines has problem. syntax function call in lisp is
(function argument…)
that means in line (i)
, you're trying call function named (setf x car(list1))
argument (if (!= x e) …)
. of course, that's not name of function, , suspect if was, didn't want call argument (if (!= x e) …)
.
(setf x car (list1))
is trying to set x
value of value of variable car
(and there isn't one), , assign new value place (list1)
. since syntax function call (function argument…)
, want instead:
(setf x (car list1))
if you're trying sequence forms, might consider using cond
, in can provide multiple forms, or progn
(see in common lisp, why multi-expression bodies of (if) statements require (progn)?).
for instance, instead of
((my_append e list2 ) ; (ii) (setf y cdr(list1)) ; (iii) (my_remove(e y list2))) ; (iv)
you want
(progn (my_append e list2 ) ; (ii) (setf y cdr(list1)) ; (iii) (my_remove(e y list2))) ; (iv)
you'll have problems that, too, though. in (iii)
, (iv)
, you'll need use (cdr list1)
, (my_remove e y list2)
, discussed above, have problem you're evaluating (ii)
, (iii)
, you're discarding value.
a simplified approach
i think might benefit if think simple definition of my-remove
. in general, list either empty list ()
or cons cell car first element of list , cdr rest of list. can use definition this, then:
- remove(x,list)
- if list empty, return list (it's empty, doesn't contain x)
- if list not empty,
- if first element of list x, return remove(x,rest(list))
- else, first element of list not x, return new list first element first element of list, , rest remove(x,rest(list)).
in code, looks like:
(defun my-remove (element list) (if (endp list) list (if (eql element (first list)) (my-remove element (rest list)) (list* (first list) (my-remove element (rest list))))))
cl-user> (my-remove 1 '(1 2 3 1 2 3)) (2 3 2 3)
those nested if
s bit ugly, , might want use cond
here, though don't need multiple expression bodies permits:
(defun my-remove (element list) (cond ((endp list) list) ((eql element (first list)) (my-remove element (rest list))) (t (list* (first list) (my-remove element (rest list))))))
since cond
clause no body test form evaluates true returns value of test form, can make last clause bit simpler:
(defun my-remove (element list) (cond ((endp list) list) ((eql element (first list)) (my-remove element (rest list))) ((list* (first list) (my-remove element (rest list))))))
Comments
Post a Comment