How can I write a MEMBER function using the DO macro in Common Lisp? -
i trying make function works member function in common lisp. so, want make function work :
(my-member 2 '(1 4 5 5 3 2 5 6 9)) => (2 5 6 9) this same how member function works.;
(member 2 '(1 4 5 5 3 2 5 6 9)) => (2 5 6 9) the condition should use 'do' macro make function. code have written make function :
(defun my-member (item x) "this function works member function." (do ((z x (rest z)) (e (first x) (first z))) (:when (equal item (first z)) (return z)))) but doesn't work..
(my-member 2 '(3 4 5 2 1 1)) -> (3 4 5 2 1 1) what should solve problem?
here correct way use do:
(do ((var 0 (1+ var)) (lst '() (cons var lst))) ((= var 5) lst))) ; ==> (4 3 2 1 0) so both var , lst variables initialised 0 , () , after each iteration variable set (1+ var) , (cons var lst).
what determines when should stop (= var 5) becoming not nil , when happens result of whole do form lst. second part of do , last has since not supply body.
you can make equivalent of member function using 1 variable , second part end condition , should result of do. luck!
Comments
Post a Comment