From: Sherif Elbanhawy
Subject: Help...Introduction to lisp
Date: 
Message-ID: <3cbba617_7@Usenet.com>
Hey guys this is me again, thanks with all the help with the first problem i
had. now I had another question...

I am trying to have a function that will convert the element of this nested
list to X's
This is what I got so far

(defun replace (L)
(cond
((null L) nil)
((atom L) (setf L 'X))
(t (list (replace (car L)) (replace (cdr L))))))

this is working but the problem is when I give it a list like this:

(a b d e f r t)

it returns:

(X (X (X (X (X (X (X NIL)))))))

Instead of what I want which is :

(X X X X X X X)

I know the the problem is in the list finction I am using...What should I
use instead and is there anything else that I need to do??

Thanks a lot

Sherif



 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com

From: Kent M Pitman
Subject: Re: Help...Introduction to lisp
Date: 
Message-ID: <sfwpu10ntt7.fsf@shell01.TheWorld.com>
"Sherif Elbanhawy" <················@hotmail.com> writes:

> This is what I got so far
> 
> (defun replace (L)
> (cond
> ((null L) nil)
> ((atom L) (setf L 'X))
> (t (list (replace (car L)) (replace (cdr L))))))

Learn to indent your programs.

Correct programs are not indented this way.

No Lisp programmer wants to read code written this way.

No open paren should appear underneath nor to the left of a containing
paren.

In general, when inside the body of a function body or a binding form,
move over two spaces.  Otherwise indent under sibling forms.  

In general, never put a newline after "(cond".  Always start the first
clause on the same line.

Without regard to whether the code is correct, the following is your
above code correctly indented:

(defun replace (L)
  (cond ((null L) nil)
        ((atom L) (setf L 'X))
        (t (list (replace (car L)) (replace (cdr L))))))

If you do not start making an attempt to indent your code correctly,
you can count on me and others not to bother responding to your posts.
Code improperly indented is too hard to read.

Also, PLEASE start a new thread for new questions with a subject line
that is suggestive of the thread content.  "Help" is almost never a good
subject line.  Neither is "Introduction to lisp".
From: Christopher C. Stacy
Subject: Re: Help...Introduction to lisp
Date: 
Message-ID: <uvgasktpl.fsf@grant.org>
>>>>> On Tue, 16 Apr 2002 04:36:36 GMT, Kent M Pitman ("Kent") writes:
 Kent> Also, PLEASE start a new thread for new questions with a subject line
 Kent> that is suggestive of the thread content.  "Help" is almost never a good
 Kent> subject line.  Neither is "Introduction to lisp".

The problem is that "Introduction to Lisp" sounds the thread that the
coursework (or self-study work) for the course titled, "Introduction to Lisp"!
A better name for this thread would have been something like "How I Learned Lisp",
but unfortunately there is no way to go back and edit history to fix that.
From: Jochen Schmidt
Subject: Re: Help...Introduction to lisp
Date: 
Message-ID: <a9gaa6$em9$1@rznews2.rrze.uni-erlangen.de>
Sherif Elbanhawy wrote:

> Hey guys this is me again, thanks with all the help with the first problem
> i had. now I had another question...
> 
> I am trying to have a function that will convert the element of this
> nested list to X's
> This is what I got so far
> 
> (defun replace (L)
> (cond
> ((null L) nil)
> ((atom L) (setf L 'X))
> (t (list (replace (car L)) (replace (cdr L))))))

You should not call that function replace if the symbol CL:REPLACE (which 
is the name of a standard function in Common Lisp) is accessible in your 
current package.

Another issue with your code above is that you do not need the (setf L 'X)
since you do not use L after that point. You can simply return 'X.

> this is working but the problem is when I give it a list like this:
> 
> (a b d e f r t)
> 
> it returns:
> 
> (X (X (X (X (X (X (X NIL)))))))
> 
> Instead of what I want which is :
> 
> (X X X X X X X)
> 
> I know the the problem is in the list finction I am using...What should I
> use instead and is there anything else that I need to do??

You are right that the list function is your problem there. I write here a
version with the above issues resolved and only that last problem left:

(defun replace-by-x (tree)
 (cond ((null tree) nil)
       ((atom tree) 'X)
       (t (list (replace-by-x (first tree))
                (replace-by-x (rest tree))))))

Think about the problem:
To replace all atoms in a nested list by X, replace the head of the list
and do the same for the rest of the list. Now you need a thing to combine 
the results of both calls to replace. How is the head related to the rest 
of the list? Your code assumes that the rest of the list is the _one_  
element following the head.

ciao,
Jochen


--
http://www.dataheaven.de
From: Sherif Elbanhawy
Subject: Re: Help...Introduction to lisp
Date: 
Message-ID: <3cbbd483_3@Usenet.com>
Jochen,

Thanks, I got it, CONS.

All, sorry about the indentions, I will learn to do that in the future:)


"Jochen Schmidt" <···@dataheaven.de> wrote in message
·················@rznews2.rrze.uni-erlangen.de...
> Sherif Elbanhawy wrote:
>
> > Hey guys this is me again, thanks with all the help with the first
problem
> > i had. now I had another question...
> >
> > I am trying to have a function that will convert the element of this
> > nested list to X's
> > This is what I got so far
> >
> > (defun replace (L)
> > (cond
> > ((null L) nil)
> > ((atom L) (setf L 'X))
> > (t (list (replace (car L)) (replace (cdr L))))))
>
> You should not call that function replace if the symbol CL:REPLACE (which
> is the name of a standard function in Common Lisp) is accessible in your
> current package.
>
> Another issue with your code above is that you do not need the (setf L 'X)
> since you do not use L after that point. You can simply return 'X.
>
> > this is working but the problem is when I give it a list like this:
> >
> > (a b d e f r t)
> >
> > it returns:
> >
> > (X (X (X (X (X (X (X NIL)))))))
> >
> > Instead of what I want which is :
> >
> > (X X X X X X X)
> >
> > I know the the problem is in the list finction I am using...What should
I
> > use instead and is there anything else that I need to do??
>
> You are right that the list function is your problem there. I write here a
> version with the above issues resolved and only that last problem left:
>
> (defun replace-by-x (tree)
>  (cond ((null tree) nil)
>        ((atom tree) 'X)
>        (t (list (replace-by-x (first tree))
>                 (replace-by-x (rest tree))))))
>
> Think about the problem:
> To replace all atoms in a nested list by X, replace the head of the list
> and do the same for the rest of the list. Now you need a thing to combine
> the results of both calls to replace. How is the head related to the rest
> of the list? Your code assumes that the rest of the list is the _one_
> element following the head.
>
> ciao,
> Jochen
>
>
> --
> http://www.dataheaven.de



 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com