From: ··············@gmail.com
Subject: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <b13f4991-3c30-447d-92cc-85005e0bd9bf@s12g2000yqi.googlegroups.com>
hi everybody. I try to resolve this exercise but my solution is not so
correct.
the exercise says:
write a fun sub-splice.
(sub-splice '(1 2) 'b '(a b c))  ---------returns------------
>            (a 1 2 c)

the solution can be recursive or iterative

I try the recursive solution:

(defun sub-splice (e x l)
              (cond ((equal x l) e)
                    ((atom l) l)
                    (t (cons (sub-splice e x (car l))
                          (sub-splice e x (cdr l))))))

but so my fun returns (a (1 2) c) and so it is not correct.

thanks for your help.

From: Pascal J. Bourguignon
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <87k53o56m4.fsf@galatea.local>
··············@gmail.com writes:

> hi everybody. I try to resolve this exercise but my solution is not so
> correct.
> the exercise says:
> write a fun sub-splice.
> (sub-splice '(1 2) 'b '(a b c))  ---------returns------------
>>            (a 1 2 c)
>
> the solution can be recursive or iterative
>
> I try the recursive solution:
>
> (defun sub-splice (e x l)
>               (cond ((equal x l) e)
>                     ((atom l) l)
>                     (t (cons (sub-splice e x (car l))
>                              (sub-splice e x (cdr l))))))
>
> but so my fun returns (a (1 2) c) and so it is not correct.

Try:

    (sub-splice '(1 2) 'a '(a b c))

What function call in your sub-splice function return the wrong result?
What other function would return the right result?
Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?


-- 
__Pascal Bourguignon__
From: ··············@gmail.com
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <a208c94b-4d21-40e7-95b2-05f930430a96@r16g2000vbn.googlegroups.com>
On 7 Giu, 18:52, ····@informatimago.com (Pascal J. Bourguignon) wrote:
> ··············@gmail.com writes:
> > hi everybody. I try to resolve this exercise but my solution is not so
> > correct.
> > the exercise says:
> > write a fun sub-splice.
> > (sub-splice '(1 2) 'b '(a b c))  ---------returns------------
> >>            (a 1 2 c)
>
> > the solution can be recursive or iterative
>
> > I try the recursive solution:
>
> > (defun sub-splice (e x l)
> >               (cond ((equal x l) e)
> >                     ((atom l) l)
> >                     (t (cons (sub-splice e x (car l))
> >                              (sub-splice e x (cdr l))))))
>
> > but so my fun returns (a (1 2) c) and so it is not correct.
>
> Try:
>
>     (sub-splice '(1 2) 'a '(a b c))
>
> What function call in your sub-splice function return the wrong result?
> What other function would return the right result?
> Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?
>
> --
> __Pascal Bourguignon__

if i try (sub-splice '(1 2) 'b '(a b c)) with my definition i obtain
(a (1 2) c) instead of (a 1 2 c)...my function becomes correct if the
first element is not a list but an atom. for example, with my
definition:

(sub-splice 1 'b '(a b c)) returns  (a 1 c) that is correct but the
exercise wants me to substitues elements of a list at the place of an
atom.

If i try (sub-splice '(1 2) 'a '(a b c)) with my fun the result is
always incorrect:

((1 2) b c)
From: Pascal J. Bourguignon
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <87fxeb6hvh.fsf@galatea.local>
··············@gmail.com writes:

> On 7 Giu, 18:52, ····@informatimago.com (Pascal J. Bourguignon) wrote:
>> ··············@gmail.com writes:
>> > hi everybody. I try to resolve this exercise but my solution is not so
>> > correct.
>> > the exercise says:
>> > write a fun sub-splice.
>> > (sub-splice '(1 2) 'b '(a b c)) �---------returns------------
>> >> � � � � � �(a 1 2 c)
>>
>> > the solution can be recursive or iterative
>>
>> > I try the recursive solution:
>>
>> > (defun sub-splice (e x l)
>> > � � � � � � � (cond ((equal x l) e)
>> > � � � � � � � � � � ((atom l) l)
>> > � � � � � � � � � � (t (cons (sub-splice e x (car l))
>> > � � � � � � � � � � � � � � �(sub-splice e x (cdr l))))))
>>
>> > but so my fun returns (a (1 2) c) and so it is not correct.
>>
>> Try:
>>
>> � � (sub-splice '(1 2) 'a '(a b c))
>>
>> What function call in your sub-splice function return the wrong result?
>> What other function would return the right result?
>> Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?
>>
>> --
>> __Pascal Bourguignon__
>
> if i try (sub-splice '(1 2) 'b '(a b c)) with my definition i obtain
> (a (1 2) c) instead of (a 1 2 c)...

I know, you already told us in your original post.

> my function becomes correct if the
> first element is not a list but an atom. for example, with my
> definition:
>
> (sub-splice 1 'b '(a b c)) returns  (a 1 c) that is correct but the
> exercise wants me to substitues elements of a list at the place of an
> atom.

I know, this is not what you asked in your original post.


> If i try (sub-splice '(1 2) 'a '(a b c)) with my fun the result is
> always incorrect:
>
> ((1 2) b c)

Ok. Now, try to think about the questions I asked:

>> What function call in your sub-splice function return the wrong result?
>> What other function would return the right result?
>> Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?

-- 
__Pascal Bourguignon__
From: ··············@gmail.com
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <aef56bed-ed24-4aa0-b3fe-a3fe2b291551@a36g2000yqc.googlegroups.com>
On 7 Giu, 20:03, ····@informatimago.com (Pascal J. Bourguignon) wrote:
> ··············@gmail.com writes:
> > On 7 Giu, 18:52, ····@informatimago.com (Pascal J. Bourguignon) wrote:
> >> ··············@gmail.com writes:
> >> > hi everybody. I try to resolve this exercise but my solution is not so
> >> > correct.
> >> > the exercise says:
> >> > write a fun sub-splice.
> >> > (sub-splice '(1 2) 'b '(a b c))  ---------returns------------
> >> >>            (a 1 2 c)
>
> >> > the solution can be recursive or iterative
>
> >> > I try the recursive solution:
>
> >> > (defun sub-splice (e x l)
> >> >               (cond ((equal x l) e)
> >> >                     ((atom l) l)
> >> >                     (t (cons (sub-splice e x (car l))
> >> >                              (sub-splice e x (cdr l))))))
>
> >> > but so my fun returns (a (1 2) c) and so it is not correct.
>
> >> Try:
>
> >>     (sub-splice '(1 2) 'a '(a b c))
>
> >> What function call in your sub-splice function return the wrong result?
> >> What other function would return the right result?
> >> Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?
>
> >> --
> >> __Pascal Bourguignon__
>
> > if i try (sub-splice '(1 2) 'b '(a b c)) with my definition i obtain
> > (a (1 2) c) instead of (a 1 2 c)...
>
> I know, you already told us in your original post.
>
> > my function becomes correct if the
> > first element is not a list but an atom. for example, with my
> > definition:
>
> > (sub-splice 1 'b '(a b c)) returns  (a 1 c) that is correct but the
> > exercise wants me to substitues elements of a list at the place of an
> > atom.
>
> I know, this is not what you asked in your original post.
>
> > If i try (sub-splice '(1 2) 'a '(a b c)) with my fun the result is
> > always incorrect:
>
> > ((1 2) b c)
>
> Ok. Now, try to think about the questions I asked:
>
> >> What function call in your sub-splice function return the wrong result?
> >> What other function would return the right result?
> >> Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?
>
> --
> __Pascal Bourguignon__

What function call in your sub-splice function return the wrong
result?

i think the call ((atom l) l)

maybe here i've to put a iteration on the list i must substitue.
From: Pascal J. Bourguignon
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <87bpoz6fqd.fsf@galatea.local>
··············@gmail.com writes:

> On 7 Giu, 20:03, ····@informatimago.com (Pascal J. Bourguignon) wrote:
>> ··············@gmail.com writes:
>> > On 7 Giu, 18:52, ····@informatimago.com (Pascal J. Bourguignon) wrote:
>> >> ··············@gmail.com writes:
>> >> > hi everybody. I try to resolve this exercise but my solution is not so
>> >> > correct.
>> >> > the exercise says:
>> >> > write a fun sub-splice.
>> >> > (sub-splice '(1 2) 'b '(a b c)) �---------returns------------
>> >> >> � � � � � �(a 1 2 c)
>>
>> >> > the solution can be recursive or iterative
>>
>> >> > I try the recursive solution:
>>
>> >> > (defun sub-splice (e x l)
>> >> > � � � � � � � (cond ((equal x l) e)
>> >> > � � � � � � � � � � ((atom l) l)
>> >> > � � � � � � � � � � (t (cons (sub-splice e x (car l))
>> >> > � � � � � � � � � � � � � � �(sub-splice e x (cdr l))))))
>>
>> >> > but so my fun returns (a (1 2) c) and so it is not correct.
>>
>> >> Try:
>>
>> >> � � (sub-splice '(1 2) 'a '(a b c))
>>
>> >> What function call in your sub-splice function return the wrong result?
>> >> What other function would return the right result?
>> >> Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?
>>
>> >> --
>> >> __Pascal Bourguignon__
>>
>> > if i try (sub-splice '(1 2) 'b '(a b c)) with my definition i obtain
>> > (a (1 2) c) instead of (a 1 2 c)...
>>
>> I know, you already told us in your original post.
>>
>> > my function becomes correct if the
>> > first element is not a list but an atom. for example, with my
>> > definition:
>>
>> > (sub-splice 1 'b '(a b c)) returns �(a 1 c) that is correct but the
>> > exercise wants me to substitues elements of a list at the place of an
>> > atom.
>>
>> I know, this is not what you asked in your original post.
>>
>> > If i try (sub-splice '(1 2) 'a '(a b c)) with my fun the result is
>> > always incorrect:
>>
>> > ((1 2) b c)
>>
>> Ok. Now, try to think about the questions I asked:
>>
>> >> What function call in your sub-splice function return the wrong result?
>> >> What other function would return the right result?
>> >> Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?
>>
>> --
>> __Pascal Bourguignon__
>
> What function call in your sub-splice function return the wrong
> result?
>
> i think the call ((atom l) l)
>
> maybe here i've to put a iteration on the list i must substitue.

This is not a function call.
Remember that in Common Lisp, there's only two syntaxes to call a function:

- either it's a named function and you call it by putting its name in
  the first position, and the arguments in the remainding positions:
    (name-of-the-function arguments...)

- or it's an anonymous function, and you call it by putting it in the
  first position, and the arguments in the remainding positions:
    ((lambda (parameter...) ...) arguments...)


((atom l) l) doesn't follow these patterns, so in no way can it be a
function call.


In fact, ((atom l) l) is a clause of a COND form.  When COND reaches
this clause, it evaluates (atom l) and if it returns true, it
evaluates the remaining forms, in this case only L.  This is not a
function call.  But assuming it was this clause that would return the
result, it would return an atom, not the cons ((1 2) . (b c)) = ((1 2) b c).


So,  try again:

 What function call in your sub-splice function returns the wrong result?
 What other function would return the right result?
 Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?

-- 
__Pascal Bourguignon__
From: ··············@gmail.com
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <fa32ef6b-6a1d-43df-8fb9-207f3041bef1@h2g2000yqg.googlegroups.com>
On 7 Giu, 20:50, ····@informatimago.com (Pascal J. Bourguignon) wrote:
> ··············@gmail.com writes:
> > On 7 Giu, 20:03, ····@informatimago.com (Pascal J. Bourguignon) wrote:
> >> ··············@gmail.com writes:
> >> > On 7 Giu, 18:52, ····@informatimago.com (Pascal J. Bourguignon) wrote:
> >> >> ··············@gmail.com writes:
> >> >> > hi everybody. I try to resolve this exercise but my solution is not so
> >> >> > correct.
> >> >> > the exercise says:
> >> >> > write a fun sub-splice.
> >> >> > (sub-splice '(1 2) 'b '(a b c))  ---------returns------------
> >> >> >>            (a 1 2 c)
>
> >> >> > the solution can be recursive or iterative
>
> >> >> > I try the recursive solution:
>
> >> >> > (defun sub-splice (e x l)
> >> >> >               (cond ((equal x l) e)
> >> >> >                     ((atom l) l)
> >> >> >                     (t (cons (sub-splice e x (car l))
> >> >> >                              (sub-splice e x (cdr l))))))
>
> >> >> > but so my fun returns (a (1 2) c) and so it is not correct.
>
> >> >> Try:
>
> >> >>     (sub-splice '(1 2) 'a '(a b c))
>
> >> >> What function call in your sub-splice function return the wrong result?
> >> >> What other function would return the right result?
> >> >> Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?
>
> >> >> --
> >> >> __Pascal Bourguignon__
>
> >> > if i try (sub-splice '(1 2) 'b '(a b c)) with my definition i obtain
> >> > (a (1 2) c) instead of (a 1 2 c)...
>
> >> I know, you already told us in your original post.
>
> >> > my function becomes correct if the
> >> > first element is not a list but an atom. for example, with my
> >> > definition:
>
> >> > (sub-splice 1 'b '(a b c)) returns  (a 1 c) that is correct but the
> >> > exercise wants me to substitues elements of a list at the place of an
> >> > atom.
>
> >> I know, this is not what you asked in your original post.
>
> >> > If i try (sub-splice '(1 2) 'a '(a b c)) with my fun the result is
> >> > always incorrect:
>
> >> > ((1 2) b c)
>
> >> Ok. Now, try to think about the questions I asked:
>
> >> >> What function call in your sub-splice function return the wrong result?
> >> >> What other function would return the right result?
> >> >> Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?
>
> >> --
> >> __Pascal Bourguignon__
>
> > What function call in your sub-splice function return the wrong
> > result?
>
> > i think the call ((atom l) l)
>
> > maybe here i've to put a iteration on the list i must substitue.
>
> This is not a function call.
> Remember that in Common Lisp, there's only two syntaxes to call a function:
>
> - either it's a named function and you call it by putting its name in
>   the first position, and the arguments in the remainding positions:
>     (name-of-the-function arguments...)
>
> - or it's an anonymous function, and you call it by putting it in the
>   first position, and the arguments in the remainding positions:
>     ((lambda (parameter...) ...) arguments...)
>
> ((atom l) l) doesn't follow these patterns, so in no way can it be a
> function call.
>
> In fact, ((atom l) l) is a clause of a COND form.  When COND reaches
> this clause, it evaluates (atom l) and if it returns true, it
> evaluates the remaining forms, in this case only L.  This is not a
> function call.  But assuming it was this clause that would return the
> result, it would return an atom, not the cons ((1 2) . (b c)) = ((1 2) b c).
>
> So,  try again:
>
>  What function call in your sub-splice function returns the wrong result?
>  What other function would return the right result?
>  Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?
>
> --
> __Pascal Bourguignon__

ok...
maybe the wrong part is

(sub-splice e x (car l)

when i call it with the argument e = '(1 2) x = 'b l = (b c) [the
second recursion]

here it would be correct to append the list with the cdr of l...
From: Pascal J. Bourguignon
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <7c3aabnlei.fsf@pbourguignon.anevia.com>
··············@gmail.com writes:
>> >> >> > (defun sub-splice (e x l)
>> >> >> > � � � � � � � (cond ((equal x l) e)
>> >> >> > � � � � � � � � � � ((atom l) l)
>> >> >> > � � � � � � � � � � (t (cons (sub-splice e x (car l))
>> >> >> > � � � � � � � � � � � � � � �(sub-splice e x (cdr l))))))
>>
> maybe the wrong part is
>
> (sub-splice e x (car l)
>
> when i call it with the argument e = '(1 2) x = 'b l = (b c) [the
> second recursion]

This is not the wrong part.  As you mentionned in other answers, CONS
is the problem.

> here it would be correct to append the list with the cdr of l...

Indeed, if you APPEND-ed one list to the other, it would give better results.


Now I see that your function has also another problem:  it returns
different things.  In some cases, it returns an atom, in other cases
it returns a list.  Also it seems to expect different things:
sometimes L is expected to be a list, sometimes it's expected to be an
atom.  These inconsistencies are the source of your problem, it's the
reason you cannot choose between CONS and APPEND.


If you started with a strictlier specification, it would be easier to
make it work:

(defun sub-splice (replacement-list  symbol  list)
   "Returns a list similar to LIST where the first occurence of SYMBOL 
is replaced by REPLACEMENT-LIST.
   (assert (listp replacement-list))
   (assert (and (symbolp symbol) (not (null symbol))))
   (assert (listp list))
   (cond
      ...
      ))

It would be useless to test (equal symbol list) since a list  is never
a non-null symbol.

And since sub-splice would always return a list, you couldn't use
CONS, but APPEND to concatenate the two lists returned by the
recursive calls to sub-splice.

You will have to do other tests...


-- 
__Pascal Bourguignon__
From: ··············@gmail.com
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <89805c6e-7f8f-4677-b41d-578fe0f326c1@x5g2000yqk.googlegroups.com>
On 8 Giu, 11:07, ····@informatimago.com (Pascal J. Bourguignon) wrote:
> ··············@gmail.com writes:
> >> >> >> > (defun sub-splice (e x l)
> >> >> >> >               (cond ((equal x l) e)
> >> >> >> >                     ((atom l) l)
> >> >> >> >                     (t (cons (sub-splice e x (car l))
> >> >> >> >                              (sub-splice e x (cdr l))))))
>
> > maybe the wrong part is
>
> > (sub-splice e x (car l)
>
> > when i call it with the argument e = '(1 2) x = 'b l = (b c) [the
> > second recursion]
>
> This is not the wrong part.  As you mentionned in other answers, CONS
> is the problem.
>
> > here it would be correct to append the list with the cdr of l...
>
> Indeed, if you APPEND-ed one list to the other, it would give better results.
>
> Now I see that your function has also another problem:  it returns
> different things.  In some cases, it returns an atom, in other cases
> it returns a list.  Also it seems to expect different things:
> sometimes L is expected to be a list, sometimes it's expected to be an
> atom.  These inconsistencies are the source of your problem, it's the
> reason you cannot choose between CONS and APPEND.
>
> If you started with a strictlier specification, it would be easier to
> make it work:
>
> (defun sub-splice (replacement-list  symbol  list)
>    "Returns a list similar to LIST where the first occurence of SYMBOL
> is replaced by REPLACEMENT-LIST.
>    (assert (listp replacement-list))
>    (assert (and (symbolp symbol) (not (null symbol))))
>    (assert (listp list))
>    (cond
>       ...
>       ))
>
> It would be useless to test (equal symbol list) since a list  is never
> a non-null symbol.
>
> And since sub-splice would always return a list, you couldn't use
> CONS, but APPEND to concatenate the two lists returned by the
> recursive calls to sub-splice.
>
> You will have to do other tests...
>
> --
> __Pascal Bourguignon__

ok i try adding another cond clause and it works

(defun sub-splice (e x l)
  (cond ((equal x l) e)
        ((atom l) l)
        ((equal x (car l)) (append e (cdr l)))
        (t (cons (car l) (sub-splice e x (cdr l))))))

for Tamas K Papp:

Maybe you don't remember when you were a beginner in programming.
Or maybe you're so smart you don't need asking for an help.
I asked for an help, and i think a wrong beginner answer doesn't make
me "truly hopeless".
it's clear programming is not my field (i'm a musician and i'm trying
to have some programming skills for composing my music), but i don't
think this may be of interest for you.
I understand your iterative solution, and from my beginner point of
view i just try to substitute add-as-first with cons.it makes sense
for me.

I've not read the chapter you gave me, infact i'm reading wilensky
book as i say, but i will.

thanks anyway for your "help".

carlo
From: Pascal J. Bourguignon
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <7cvdn6nc68.fsf@pbourguignon.anevia.com>
··············@gmail.com writes:

> On 8 Giu, 11:07, ····@informatimago.com (Pascal J. Bourguignon) wrote:
>> ··············@gmail.com writes:
>> >> >> >> > (defun sub-splice (e x l)
>> >> >> >> > � � � � � � � (cond ((equal x l) e)
>> >> >> >> > � � � � � � � � � � ((atom l) l)
>> >> >> >> > � � � � � � � � � � (t (cons (sub-splice e x (car l))
>> >> >> >> > � � � � � � � � � � � � � � �(sub-splice e x (cdr l))))))
>>
>> > maybe the wrong part is
>>
>> > (sub-splice e x (car l)
>>
>> > when i call it with the argument e = '(1 2) x = 'b l = (b c) [the
>> > second recursion]
>>
>> This is not the wrong part. �As you mentionned in other answers, CONS
>> is the problem.
>>
>> > here it would be correct to append the list with the cdr of l...
>>
>> Indeed, if you APPEND-ed one list to the other, it would give better results.
>>
>> Now I see that your function has also another problem: �it returns
>> different things. �In some cases, it returns an atom, in other cases
>> it returns a list. �Also it seems to expect different things:
>> sometimes L is expected to be a list, sometimes it's expected to be an
>> atom. �These inconsistencies are the source of your problem, it's the
>> reason you cannot choose between CONS and APPEND.
>>
>> If you started with a strictlier specification, it would be easier to
>> make it work:
>>
>> (defun sub-splice (replacement-list �symbol �list)
>> � �"Returns a list similar to LIST where the first occurence of SYMBOL
>> is replaced by REPLACEMENT-LIST.
>> � �(assert (listp replacement-list))
>> � �(assert (and (symbolp symbol) (not (null symbol))))
>> � �(assert (listp list))
>> � �(cond
>> � � � ...
>> � � � ))
>>
>> It would be useless to test (equal symbol list) since a list �is never
>> a non-null symbol.
>>
>> And since sub-splice would always return a list, you couldn't use
>> CONS, but APPEND to concatenate the two lists returned by the
>> recursive calls to sub-splice.
>>
>> You will have to do other tests...
>>
>> --
>> __Pascal Bourguignon__
>
> ok i try adding another cond clause and it works
>
> (defun sub-splice (e x l)
>   (cond ((equal x l) e)
>         ((atom l) l)
>         ((equal x (car l)) (append e (cdr l)))
>         (t (cons (car l) (sub-splice e x (cdr l))))))


C/USER[22]> (sub-splice '(1 2) 'a '(a b c))
(1 2 B C)
C/USER[23]> (sub-splice '(1 2) 'b '(a b c))
(A 1 2 C)
C/USER[24]> (sub-splice '(1 2) 'c '(a b c))
(A B 1 2)
C/USER[25]> (sub-splice '(1 2) 'nil '(a b c))
(A B C 1 2)
C/USER[26]> (sub-splice '(1 2) 'x '(a b c))
(A B C)


Looks good.    What about the fourth result, with x = NIL ?


Is it what you expected? 


Do you understand why (sub-splice '(1 2) 'nil '(a b c)) 
doesn't return the same as  (sub-splice '(1 2) 'x '(a b c)) ?


If you wanted to make x = NIL behave like x = X, what test would have
to be removed from the COND?


In which case is (ATOM L) true?  That is, what kinds of list L would
we have to give to sub-splice for (ATOM L) to be true?  What are you
really testing here? Isn't there a more specific predicate to test
that?




-- 
__Pascal Bourguignon__
From: ··············@gmail.com
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <e2ca7b44-76c1-45f6-bedb-a29595459584@a36g2000yqc.googlegroups.com>
On 8 Giu, 14:27, ····@informatimago.com (Pascal J. Bourguignon) wrote:
> ··············@gmail.com writes:
> > On 8 Giu, 11:07, ····@informatimago.com (Pascal J. Bourguignon) wrote:
> >> ··············@gmail.com writes:
> >> >> >> >> > (defun sub-splice (e x l)
> >> >> >> >> >               (cond ((equal x l) e)
> >> >> >> >> >                     ((atom l) l)
> >> >> >> >> >                     (t (cons (sub-splice e x (car l))
> >> >> >> >> >                              (sub-splice e x (cdr l))))))
>
> >> > maybe the wrong part is
>
> >> > (sub-splice e x (car l)
>
> >> > when i call it with the argument e = '(1 2) x = 'b l = (b c) [the
> >> > second recursion]
>
> >> This is not the wrong part.  As you mentionned in other answers, CONS
> >> is the problem.
>
> >> > here it would be correct to append the list with the cdr of l...
>
> >> Indeed, if you APPEND-ed one list to the other, it would give better results.
>
> >> Now I see that your function has also another problem:  it returns
> >> different things.  In some cases, it returns an atom, in other cases
> >> it returns a list.  Also it seems to expect different things:
> >> sometimes L is expected to be a list, sometimes it's expected to be an
> >> atom.  These inconsistencies are the source of your problem, it's the
> >> reason you cannot choose between CONS and APPEND.
>
> >> If you started with a strictlier specification, it would be easier to
> >> make it work:
>
> >> (defun sub-splice (replacement-list  symbol  list)
> >>    "Returns a list similar to LIST where the first occurence of SYMBOL
> >> is replaced by REPLACEMENT-LIST.
> >>    (assert (listp replacement-list))
> >>    (assert (and (symbolp symbol) (not (null symbol))))
> >>    (assert (listp list))
> >>    (cond
> >>       ...
> >>       ))
>
> >> It would be useless to test (equal symbol list) since a list  is never
> >> a non-null symbol.
>
> >> And since sub-splice would always return a list, you couldn't use
> >> CONS, but APPEND to concatenate the two lists returned by the
> >> recursive calls to sub-splice.
>
> >> You will have to do other tests...
>
> >> --
> >> __Pascal Bourguignon__
>
> > ok i try adding another cond clause and it works
>
> > (defun sub-splice (e x l)
> >   (cond ((equal x l) e)
> >         ((atom l) l)
> >         ((equal x (car l)) (append e (cdr l)))
> >         (t (cons (car l) (sub-splice e x (cdr l))))))
>
> C/USER[22]> (sub-splice '(1 2) 'a '(a b c))
> (1 2 B C)
> C/USER[23]> (sub-splice '(1 2) 'b '(a b c))
> (A 1 2 C)
> C/USER[24]> (sub-splice '(1 2) 'c '(a b c))
> (A B 1 2)
> C/USER[25]> (sub-splice '(1 2) 'nil '(a b c))
> (A B C 1 2)
> C/USER[26]> (sub-splice '(1 2) 'x '(a b c))
> (A B C)
>
> Looks good.    What about the fourth result, with x = NIL ?
>
> Is it what you expected?
>
> Do you understand why (sub-splice '(1 2) 'nil '(a b c))
> doesn't return the same as  (sub-splice '(1 2) 'x '(a b c)) ?
>
> If you wanted to make x = NIL behave like x = X, what test would have
> to be removed from the COND?
>
> In which case is (ATOM L) true?  That is, what kinds of list L would
> we have to give to sub-splice for (ATOM L) to be true?  What are you
> really testing here? Isn't there a more specific predicate to test
> that?
>
> --
> __Pascal Bourguignon__

I've to remove the first cond test ((equal x l) e) to make x=NIL to
act like x=X

but i've also change the first cond test in ((null l) l) because it's
this the real test i want.

then i change my function in this way

(defun sub-splice (e x l)
  (cond ((null l) l)
        ((equal x (car l)) (append e (sub-splice e x (cdr l))))
        (t (cons (car l) (sub-splice e x (cdr l))))))

because before with (sub-splice '(1 2) 'c '(c b c)) it would return (1
2 b c) that it's not what i want. now it seems to me correct.
From: Pascal J. Bourguignon
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <7ciqj6nana.fsf@pbourguignon.anevia.com>
··············@gmail.com writes:

> I've to remove the first cond test ((equal x l) e) to make x=NIL to
> act like x=X
>
> but i've also change the first cond test in ((null l) l) because it's
> this the real test i want.
>
> then i change my function in this way
>
> (defun sub-splice (e x l)
>   (cond ((null l) l)
>         ((equal x (car l)) (append e (sub-splice e x (cdr l))))
>         (t (cons (car l) (sub-splice e x (cdr l))))))
>
> because before with (sub-splice '(1 2) 'c '(c b c)) it would return (1
> 2 b c) that it's not what i want. now it seems to me correct.

Great!


-- 
__Pascal Bourguignon__
From: Tamas K Papp
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <794nfgF1hk44tU1@mid.individual.net>
On Mon, 08 Jun 2009 05:00:10 -0700, carlo.ambrogio wrote:

> for Tamas K Papp:
> 
> Maybe you don't remember when you were a beginner in programming. Or
> maybe you're so smart you don't need asking for an help. I asked for an
> help, and i think a wrong beginner answer doesn't make me "truly
> hopeless".
> it's clear programming is not my field (i'm a musician and i'm trying to
> have some programming skills for composing my music), but i don't think
> this may be of interest for you. I understand your iterative solution,
> and from my beginner point of view i just try to substitute add-as-first
> with cons.it makes sense for me.
> 
> I've not read the chapter you gave me, infact i'm reading wilensky book
> as i say, but i will.
> 
> thanks anyway for your "help".

I assumed that you were trying to get a solution to a HW problem.  I
am sorry if I misunderstood.

I would suggest that you switch to the PCL book, it is better suited
for beginners (IMO).

Tamas
From: Tamas K Papp
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <794rm5F1p88ggU1@mid.individual.net>
On Mon, 08 Jun 2009 15:02:08 +0000, Tamas K Papp wrote:

> On Mon, 08 Jun 2009 05:00:10 -0700, carlo.ambrogio wrote:
> 
>> for Tamas K Papp:
>> 
>> Maybe you don't remember when you were a beginner in programming. Or
>> maybe you're so smart you don't need asking for an help. I asked for an
>> help, and i think a wrong beginner answer doesn't make me "truly
>> hopeless".
>> it's clear programming is not my field (i'm a musician and i'm trying
>> to have some programming skills for composing my music), but i don't
>> think this may be of interest for you. I understand your iterative
>> solution, and from my beginner point of view i just try to substitute
>> add-as-first with cons.it makes sense for me.
>> 
>> I've not read the chapter you gave me, infact i'm reading wilensky book
>> as i say, but i will.
>> 
>> thanks anyway for your "help".
> 
> I assumed that you were trying to get a solution to a HW problem.  I am
> sorry if I misunderstood.
> 
> I would suggest that you switch to the PCL book, it is better suited for
> beginners (IMO).

Forgot to add: your intuition was going in the right direction.  You
can implement add-as-first by replacing

(add-as-first item place)

with

(setf place (cons item place))

If you do these replacements in the code I proposed, it will work (you
still need to reverse though).  But you can't write add-as-first as a
function!  If you try:

CL-USER> (defparameter *a* '(1 2 3))
*A*
CL-USER> (defun add-as-first (item place) (setf place (cons item place)))
ADD-AS-FIRST
CL-USER> (add-as-first 0 *a*)
(0 1 2 3)

So did it work?

CL-USER> *a*
(1 2 3)

No.  This is because the place that was modified is an argument of the
function, not the original *a*.  Common Lisp works around this by
defining the equivalent of add-as-first as a macro.  You will
eventually learn about macros, I would suggest the appropriate chapter
of PCL.

Another thing.  I think I was quite rude, because I was 100% positive
that you were trying to get other people to do you homework with
minimal effort on your part.  I was wondering why I thought that, then
I realized: the exercise looks like something from a "Lisp is
manipulating lists recursively with about 10 language constructs until
you begin to hate the language" course that is still taught at some
places.  I am wondering if that is the best approach for you, and if
not, you should consider the PCL book, the whole thing is available
online.

HTH,

Tamas
From: ··············@gmail.com
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <d50fff91-0c16-4408-8b1d-34b4dd3f2e40@k8g2000yqn.googlegroups.com>
On 8 Giu, 18:13, Tamas K Papp <······@gmail.com> wrote:
> On Mon, 08 Jun 2009 15:02:08 +0000, Tamas K Papp wrote:
> > On Mon, 08 Jun 2009 05:00:10 -0700, carlo.ambrogio wrote:
>
> >> for Tamas K Papp:
>
> >> Maybe you don't remember when you were a beginner in programming. Or
> >> maybe you're so smart you don't need asking for an help. I asked for an
> >> help, and i think a wrong beginner answer doesn't make me "truly
> >> hopeless".
> >> it's clear programming is not my field (i'm a musician and i'm trying
> >> to have some programming skills for composing my music), but i don't
> >> think this may be of interest for you. I understand your iterative
> >> solution, and from my beginner point of view i just try to substitute
> >> add-as-first with cons.it makes sense for me.
>
> >> I've not read the chapter you gave me, infact i'm reading wilensky book
> >> as i say, but i will.
>
> >> thanks anyway for your "help".
>
> > I assumed that you were trying to get a solution to a HW problem.  I am
> > sorry if I misunderstood.
>
> > I would suggest that you switch to the PCL book, it is better suited for
> > beginners (IMO).
>
> Forgot to add: your intuition was going in the right direction.  You
> can implement add-as-first by replacing
>
> (add-as-first item place)
>
> with
>
> (setf place (cons item place))
>
> If you do these replacements in the code I proposed, it will work (you
> still need to reverse though).  But you can't write add-as-first as a
> function!  If you try:
>
> CL-USER> (defparameter *a* '(1 2 3))
> *A*
> CL-USER> (defun add-as-first (item place) (setf place (cons item place)))
> ADD-AS-FIRST
> CL-USER> (add-as-first 0 *a*)
> (0 1 2 3)
>
> So did it work?
>
> CL-USER> *a*
> (1 2 3)
>
> No.  This is because the place that was modified is an argument of the
> function, not the original *a*.  Common Lisp works around this by
> defining the equivalent of add-as-first as a macro.  You will
> eventually learn about macros, I would suggest the appropriate chapter
> of PCL.
>
> Another thing.  I think I was quite rude, because I was 100% positive
> that you were trying to get other people to do you homework with
> minimal effort on your part.  I was wondering why I thought that, then
> I realized: the exercise looks like something from a "Lisp is
> manipulating lists recursively with about 10 language constructs until
> you begin to hate the language" course that is still taught at some
> places.  I am wondering if that is the best approach for you, and if
> not, you should consider the PCL book, the whole thing is available
> online.
>
> HTH,
>
> Tamas

thanks again tamas, now i understand you version...i'll continue to
read the wilensky (i hate not to finish the books i begin), but then
i'll start pcl (times ago i wanted to start pcl but people in this
group suggest me this order: touretzky -- wilensky -- pcl (or lisp by
winston) and so on...)
From: Tamas K Papp
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <7932imF1or7n8U1@mid.individual.net>
On Sun, 07 Jun 2009 09:49:13 -0700, carlo.ambrogio wrote:

> hi everybody. I try to resolve this exercise but my solution is not so
> correct.
> the exercise says:
> write a fun sub-splice.
> (sub-splice '(1 2) 'b '(a b c))  ---------returns------------
>>            (a 1 2 c)
> 
> the solution can be recursive or iterative
> 
> I try the recursive solution:
> 
> (defun sub-splice (e x l)
>               (cond ((equal x l) e)
>                     ((atom l) l)
>                     (t (cons (sub-splice e x (car l))
>                           (sub-splice e x (cdr l))))))
> 
> but so my fun returns (a (1 2) c) and so it is not correct.
> 
> thanks for your help.

Pascal is helping you with a recursive solution, so I thought I would
give you a hint with an iterative one.

(defun sub-splice (replacement-list element list)
  (let ((result nil))
    (dolist (elt list)
      (if (equal elt element)
	  (dolist (elt replacement-list)
	    (add-as-first elt result))
	  (add-as-first elt result)))
    result))

It is not ready yet!  Add-as-first exists in CL, but under a different
name.  You will have to find it.  And you will discover that the list
will be in reverse order -- you will have to reverse it at the and to
get the correct result.  You should have no difficulty finding the
library function that does this.

A bit of reading will help you considerably.  Especially Chapter 12 of PCL:
http://www.gigamonkeys.com/book/they-called-it-lisp-for-a-reason-list-processing.html

HTH,

Tamas
From: ··············@gmail.com
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <7f9b17e1-4d11-4b8e-b073-b413d6fc6a26@c19g2000yqc.googlegroups.com>
On 8 Giu, 01:59, Tamas K Papp <······@gmail.com> wrote:
> On Sun, 07 Jun 2009 09:49:13 -0700, carlo.ambrogio wrote:
> > hi everybody. I try to resolve this exercise but my solution is not so
> > correct.
> > the exercise says:
> > write a fun sub-splice.
> > (sub-splice '(1 2) 'b '(a b c))  ---------returns------------
> >>            (a 1 2 c)
>
> > the solution can be recursive or iterative
>
> > I try the recursive solution:
>
> > (defun sub-splice (e x l)
> >               (cond ((equal x l) e)
> >                     ((atom l) l)
> >                     (t (cons (sub-splice e x (car l))
> >                           (sub-splice e x (cdr l))))))
>
> > but so my fun returns (a (1 2) c) and so it is not correct.
>
> > thanks for your help.
>
> Pascal is helping you with a recursive solution, so I thought I would
> give you a hint with an iterative one.
>
> (defun sub-splice (replacement-list element list)
>   (let ((result nil))
>     (dolist (elt list)
>       (if (equal elt element)
>           (dolist (elt replacement-list)
>             (add-as-first elt result))
>           (add-as-first elt result)))
>     result))
>
> It is not ready yet!  Add-as-first exists in CL, but under a different
> name.  You will have to find it.  And you will discover that the list
> will be in reverse order -- you will have to reverse it at the and to
> get the correct result.  You should have no difficulty finding the
> library function that does this.
>
> A bit of reading will help you considerably.  Especially Chapter 12 of PCL:http://www.gigamonkeys.com/book/they-called-it-lisp-for-a-reason-list...
>
> HTH,
>
> Tamas

i think add-as-first could be cons in lisp, but i tried it and it
doesn't work.it returns always nil.
From: Tamas K Papp
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <79372kF1oncmpU1@mid.individual.net>
On Sun, 07 Jun 2009 17:25:25 -0700, carlo.ambrogio wrote:

> On 8 Giu, 01:59, Tamas K Papp <······@gmail.com> wrote:
>> On Sun, 07 Jun 2009 09:49:13 -0700, carlo.ambrogio wrote:
>> > hi everybody. I try to resolve this exercise but my solution is not
>> > so correct.
>> > the exercise says:
>> > write a fun sub-splice.
>> > (sub-splice '(1 2) 'b '(a b c))  ---------returns------------
>> >>            (a 1 2 c)
>>
>> > the solution can be recursive or iterative
>>
>> > I try the recursive solution:
>>
>> > (defun sub-splice (e x l)
>> >               (cond ((equal x l) e)
>> >                     ((atom l) l)
>> >                     (t (cons (sub-splice e x (car l))
>> >                           (sub-splice e x (cdr l))))))
>>
>> > but so my fun returns (a (1 2) c) and so it is not correct.
>>
>> > thanks for your help.
>>
>> Pascal is helping you with a recursive solution, so I thought I would
>> give you a hint with an iterative one.
>>
>> (defun sub-splice (replacement-list element list)
>>   (let ((result nil))
>>     (dolist (elt list)
>>       (if (equal elt element)
>>           (dolist (elt replacement-list)
>>             (add-as-first elt result))
>>           (add-as-first elt result)))
>>     result))
>>
>> It is not ready yet!  Add-as-first exists in CL, but under a different
>> name.  You will have to find it.  And you will discover that the list
>> will be in reverse order -- you will have to reverse it at the and to
>> get the correct result.  You should have no difficulty finding the
>> library function that does this.
>>
>> A bit of reading will help you considerably.  Especially Chapter 12 of
>> PCL:http://www.gigamonkeys.com/book/they-called-it-lisp-for-a-reason-list...
>>
>> HTH,
>>
>> Tamas
> 
> i think add-as-first could be cons in lisp, but i tried it and it
> doesn't work.it returns always nil.

Boy, you are truly hopeless.  If you are a CS major or imagine
yourself in a career line where you have to program computers at all,
switch.

I gave you a link to a single chapter, if you had read that, you would
already have your solution.  Instead you keep "trying" functions in
the hope that they work.  In case you don't know already (and that's
what it looks like), programming does requires some level of
understanding of what you are doing.

Tamas
From: Pascal J. Bourguignon
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <874our5o3h.fsf@galatea.local>
··············@gmail.com writes:

> On 8 Giu, 01:59, Tamas K Papp <······@gmail.com> wrote:
>> On Sun, 07 Jun 2009 09:49:13 -0700, carlo.ambrogio wrote:
>> > hi everybody. I try to resolve this exercise but my solution is not so
>> > correct.
>> > the exercise says:
>> > write a fun sub-splice.
>> > (sub-splice '(1 2) 'b '(a b c)) �---------returns------------
>> >> � � � � � �(a 1 2 c)
>>
>> > the solution can be recursive or iterative
>>
>> > I try the recursive solution:
>>
>> > (defun sub-splice (e x l)
>> > � � � � � � � (cond ((equal x l) e)
>> > � � � � � � � � � � ((atom l) l)
>> > � � � � � � � � � � (t (cons (sub-splice e x (car l))
>> > � � � � � � � � � � � � � (sub-splice e x (cdr l))))))
>>
>> > but so my fun returns (a (1 2) c) and so it is not correct.
>>
>> > thanks for your help.
> [...]
> i think add-as-first could be cons in lisp, but i tried it and it
> doesn't work.it returns always nil.

Ok, so you have the answer to you my first question.  Indeed the
function call that produce the wrong result is (cons ... ...).

(cons '(1 2) '(b c)) --> ((1 2) . (b c)) = ((1 2) b c)

Now you can answer the remaining questions:

 What other function would return the right result?

     (??? '(1 2) '(b c)) --> (1 2 b c)

 Would this still work with: (sub-splice '(1 2) 'b '(a b c)) ?



-- 
__Pascal Bourguignon__
From: Pascal J. Bourguignon
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <87zlcj48qm.fsf@galatea.local>
··············@gmail.com writes:
> i think add-as-first could be cons in lisp, but i tried it and it
> doesn't work. it returns always nil.

No, CONS never returns NIL, by definition.

Do you use the REPL?

[···@galatea :0.0 ~]$ clisp 
  i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
  I I I I I I I      8     8   8           8     8     o  8    8
  I  \ `+' /  I      8         8           8     8        8    8
   \  `-+-'  /       8         8           8      ooooo   8oooo
    `-__|__-'        8         8           8           8  8
        |            8     o   8           8     o     8  8
  ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

Welcome to GNU CLISP 2.47 (2008-10-23) <http://clisp.cons.org/>

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2008

Type :h and hit Enter for context help.

[1]> (cons '(1 2) '(b c))
((1 2) B C)
[2]> (cons nil nil)
(NIL)
[3]> (cons nil 'a)
(NIL . A)
[4]> (cons 'a nil)
(A)
[5]> (ext:quit)
Bye.
[···@galatea :0.0 ~]$ 



-- 
__Pascal Bourguignon__
From: Pascal J. Bourguignon
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <87vdn748d7.fsf@galatea.local>
··············@gmail.com writes:
> i think add-as-first could be cons in lisp, but i tried it and it
> doesn't work.it returns always nil.

Here is how you could have asked lisp what function call returned the
wrong result.  You'd have had to ask lisp to TRACE the functions
called by sub-splice.  But since these functions are in CL, you cannot
TRACE them directly.  So first you would have to SHADOW them, and
define wrapper functions that you could TRACE:


[···@galatea :0.0 ~]$ clisp -norc
  i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
  I I I I I I I      8     8   8           8     8     o  8    8
  I  \ `+' /  I      8         8           8     8        8    8
   \  `-+-'  /       8         8           8      ooooo   8oooo
    `-__|__-'        8         8           8           8  8
        |            8     o   8           8     o     8  8
  ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

Welcome to GNU CLISP 2.47 (2008-10-23) <http://clisp.cons.org/>

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2008

Type :h and hit Enter for context help.

[1]> (shadow '(equal atom cons car cdr))
T
[2]> (defun equal (a b) (cl:equal a b))
EQUAL
[3]> (defun atom (x) (cl:atom x))
ATOM
[4]> (defun cons (a d) (cl:cons a d))
CONS
[5]> (defun car (c) (cl:car c))
CAR
[6]> (defun cdr (c) (cl:cdr c))
CDR
[7]> (trace equal atom cons car cdr)
;; Tracing function EQUAL.
;; Tracing function ATOM.
;; Tracing function CONS.
;; Tracing function CAR.
;; Tracing function CDR.
(EQUAL ATOM CONS CAR CDR)
[8]> 
(defun sub-splice (e x l)
  (cond ((equal x l) e)
        ((atom l) l)
        (t (cons (sub-splice e x (car l))
                 (sub-splice e x (cdr l))))))
SUB-SPLICE
[9]> (trace sub-splice)
;; Tracing function SUB-SPLICE.
(SUB-SPLICE)
[10]> (sub-splice '(1 2) 'a '(a b c))
1. Trace: (SUB-SPLICE '(1 2) 'A '(A B C))
2. Trace: (EQUAL 'A '(A B C))
2. Trace: EQUAL ==> NIL
2. Trace: (ATOM '(A B C))
2. Trace: ATOM ==> NIL
2. Trace: (CAR '(A B C))
2. Trace: CAR ==> A
2. Trace: (SUB-SPLICE '(1 2) 'A 'A)
3. Trace: (EQUAL 'A 'A)
3. Trace: EQUAL ==> T
2. Trace: SUB-SPLICE ==> (1 2)
2. Trace: (CDR '(A B C))
2. Trace: CDR ==> (B C)
2. Trace: (SUB-SPLICE '(1 2) 'A '(B C))
3. Trace: (EQUAL 'A '(B C))
3. Trace: EQUAL ==> NIL
3. Trace: (ATOM '(B C))
3. Trace: ATOM ==> NIL
3. Trace: (CAR '(B C))
3. Trace: CAR ==> B
3. Trace: (SUB-SPLICE '(1 2) 'A 'B)
4. Trace: (EQUAL 'A 'B)
4. Trace: EQUAL ==> NIL
4. Trace: (ATOM 'B)
4. Trace: ATOM ==> T
3. Trace: SUB-SPLICE ==> B
3. Trace: (CDR '(B C))
3. Trace: CDR ==> (C)
3. Trace: (SUB-SPLICE '(1 2) 'A '(C))
4. Trace: (EQUAL 'A '(C))
4. Trace: EQUAL ==> NIL
4. Trace: (ATOM '(C))
4. Trace: ATOM ==> NIL
4. Trace: (CAR '(C))
4. Trace: CAR ==> C
4. Trace: (SUB-SPLICE '(1 2) 'A 'C)
5. Trace: (EQUAL 'A 'C)
5. Trace: EQUAL ==> NIL
5. Trace: (ATOM 'C)
5. Trace: ATOM ==> T
4. Trace: SUB-SPLICE ==> C
4. Trace: (CDR '(C))
4. Trace: CDR ==> NIL
4. Trace: (SUB-SPLICE '(1 2) 'A 'NIL)
5. Trace: (EQUAL 'A 'NIL)
5. Trace: EQUAL ==> NIL
5. Trace: (ATOM 'NIL)
5. Trace: ATOM ==> T
4. Trace: SUB-SPLICE ==> NIL
4. Trace: (CONS 'C 'NIL)
4. Trace: CONS ==> (C)
3. Trace: SUB-SPLICE ==> (C)
3. Trace: (CONS 'B '(C))
3. Trace: CONS ==> (B C)
2. Trace: SUB-SPLICE ==> (B C)
2. Trace: (CONS '(1 2) '(B C))
2. Trace: CONS ==> ((1 2) B C)
1. Trace: SUB-SPLICE ==> ((1 2) B C)
((1 2) B C)
[11]> 

So we can see that the wrong result is produced by (CONS '(1 2) '(B C)).


-- 
__Pascal Bourguignon__
From: Thomas A. Russ
Subject: Re: execise n.7 cap 6 wilensky book
Date: 
Message-ID: <ymid49ezkxp.fsf@blackcat.isi.edu>
··············@gmail.com writes:

> hi everybody. I try to resolve this exercise but my solution is not so
> correct.
> the exercise says:
> write a fun sub-splice.
> (sub-splice '(1 2) 'b '(a b c))  ---------returns------------
> >            (a 1 2 c)
> 
> the solution can be recursive or iterative
> 
> I try the recursive solution:

I note that there are solutions in other parts of this thread.  I would
like to just make a larger, over-arching comment about one goal of this
exercise.

In your original attempt at a solution, you ended up with doing a
straight substition of one item for the other.  This is like the
built-in SUBST function, and is often used as an initial exercise in
list and cons maniputlation.

That works easily since there really is a one-for-one substitution, so
you can do it quite locally.  You just find the right thing to return.
This preserves the original list structure but with some new values.

The task in SUB-SPLICE is to alter the list structure by splicing in the
new value.  This splicing requires you to "go up one level" in the list
structure walker so that you can alter the actual structure rather than
just follow it.  That is the primary purpose of this exercise, to show
how you have to work one level further up in the list structure.

-- 
Thomas A. Russ,  USC/Information Sciences Institute