·············@gmail.com wrote:
> Do you suggest any books, places...
> where I could learn more about recursion?
I have a short tutorial online:
http://www.prairienet.org/~dsb/recursion.htm
> (defun delete-in (e l)
> (cond ((null l) nil)
> ((equal e (car l)) (delete-in e (cdr l)))
> ((atom (car l)) (cons (car l) (delete-in e (cdr l))))
> (t (cons (delete-in e (car l))
> (delete-in e (cdr l))))))
> (defun delete-in (e l)
> (cond ((null l) nil)
> ((equal e l) nil)
> ((atom l) l)
> (t (cons (delete-in e (car l))
> (delete-in e (cdr l))))))
> Is there a way to modify the second one so that it works?
Partially. You can check to see if l is an atom, as
your second version does, but if l is a non-null list,
you still have to decide whether or not to throw out
(car l) as the first version does. You can also combine
the recursive calls on (cdr l) by doing them/it first.
(defun delete-in (e l)
(cond
((null l) nil)
((atom l) l )
(t (let ((tail (delete-in e (cdr l))))
(if (equal (car l) e)
tail
(cons (delete-in e (car l))
tail))))))
By the way, in case you're interested in other functional-
programming tricks, there's also a neat mapped solution:
(defun map-delete-in (e l)
(if (atom l) l
(mapcar (lambda (l) (map-delete-in e l))
(delete e l))))
--
Dan
www.prairienet.org/~dsb/