From: ·············@gmail.com
Subject: Remove every atom
Date: 
Message-ID: <1187106286.566439.177610@i38g2000prf.googlegroups.com>
Hi everyone,

I'm still new to Lisp. I've been learning Lisp for quite a while and
still having lots of difficulties with recursion. Do you suggest any
books, places... where I could learn more about recursion?


As well, I'm doing this Lisp exercise (not a homework problem). This
is
to remove an atom from all levels of a list.
It's supposed to work this way:


(delete-in 'a '(a b (d (c a)) a d))
(B (D (C)) D)


I worked out this solution:

(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))))))


Then I tried to code another way:

(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))))))


Of course the result is not right:

 (delete-in 'a '(a b (d (c a)) a d))
(NIL B (D (C NIL)) NIL D)


How should I think about these two ways of recursion for doing the
same thing? Is there a way to modify the
second one so that it works?


Thanks.


Mark.

From: Kaz Kylheku
Subject: Re: Remove every atom
Date: 
Message-ID: <1187111102.351429.23450@19g2000hsx.googlegroups.com>
On Aug 14, 8:44 am, ·············@gmail.com wrote:
> Hi everyone,
>
> I'm still new to Lisp. I've been learning Lisp for quite a while and
> still having lots of difficulties with recursion. Do you suggest any
> books, places... where I could learn more about recursion?
>
> As well, I'm doing this Lisp exercise (not a homework problem). This
> is
> to remove an atom from all levels of a list.
> It's supposed to work this way:
>
> (delete-in 'a '(a b (d (c a)) a d))
> (B (D (C)) D)
>
> I worked out this solution:
>
> (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))))))
>
> Then I tried to code another way:
>
> (defun delete-in (e l)
>   (cond ((null l) nil)
>         ((equal e l) nil)

Since E is supposedly an atom and L is a list, the only way (EQUAL E
L) can ever be true is if E is the atom NIL and L is the empty list.

>         ((atom l) l)

The only way (ATOM L) can be true is if L is the empty list, or isn't
a list at all.

>         (t (cons (delete-in e (car l))
>                  (delete-in e (cdr l))))))
>
> Of course the result is not right:
>
>  (delete-in 'a '(a b (d (c a)) a d))
> (NIL B (D (C NIL)) NIL D)
>
> How should I think about these two ways of recursion for doing the
> same thing?

You should think about them as being completely different functions
that do different things.

> Is there a way to modify the
> second one so that it works?

Yes; edit it little by little, adding and remove material such that
you gradually eliminate the difference between it and the first
working one. :)
From: Pillsy
Subject: Re: Remove every atom
Date: 
Message-ID: <1187268015.926569.73800@r34g2000hsd.googlegroups.com>
On Aug 14, 11:44 am, ·············@gmail.com wrote:

> Do you suggest any books, places... where I could learn more
> about recursion?

When I was first learning Lisp, I found two books particularly helpful
for this:

(1) David S. Touretzky's /Common Lisp: A Gentle Introduction to
Symbolic Programming/, available free at http://www.cs.cmu.edu/~dst/LispBook/index.html
. If you're coming to Lisp with only a very basic knowledge of
programming, this book is (IMO) the one to start with.

(2) Stuart C. Shapiro's /Common Lisp: An Interactive Approach/,
available free at http://www.cse.buffalo.edu/~shapiro/Commonlisp/

These books have two major advantages. The first is that they're
available free online, and the second, more significant advantage is
that they present all this stuff in the context of Common Lisp, so you
don't have to learn another language to learn about recursion.

Cheers,
Pillsy