Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Simple newbie list processing troubble
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 36 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Peder Y  
View profile  
 More options Sep 28 2002, 5:04 am
Newsgroups: comp.lang.lisp
From: pyda...@ec.auckland.ac.nz (Peder Y)
Date: 28 Sep 2002 02:04:57 -0700
Local: Sat, Sep 28 2002 5:04 am
Subject: Simple newbie list processing troubble
Say I have a function foo that is supposed to take a list as input (A
B C D...). The function will then traverse the list looking for an
element, and change that element. That's basically it.

For example, say, the function shall set all elements of lst to 'A:
Why is this wrong?

(defun foo (lst)
  (loop for x in lst do
                  (setf (car x) 'A)))

- Peder -


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Warner  
View profile  
 More options Sep 28 2002, 7:07 am
Newsgroups: comp.lang.lisp
From: "Adam Warner" <use...@consulting.net.nz>
Date: Sat, 28 Sep 2002 23:06:58 +1200
Local: Sat, Sep 28 2002 7:06 am
Subject: Re: Simple newbie list processing troubble
Hi Peder Y,

> Say I have a function foo that is supposed to take a list as input (A
> B C D...). The function will then traverse the list looking for an
> element, and change that element. That's basically it.

> For example, say, the function shall set all elements of lst to 'A:
> Why is this wrong?

> (defun foo (lst)
>   (loop for x in lst do
>              (setf (car x) 'A)))

Unless you have a particular need to perform destructive modification of
the input list Peder just create another list:

(defun foo (list)
   (loop for x in list collect 'A))

You tried to find the car of a symbol. As you loop through the list x
contains elements of the list.

[1]> (setf list '(a b c d))
(A B C D)
[2]> (listp list)
T
[3]> (listp (first list))
NIL

Regards,
Adam


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul F. Dietz  
View profile  
 More options Sep 28 2002, 8:10 am
Newsgroups: comp.lang.lisp
From: "Paul F. Dietz" <di...@dls.net>
Date: Sat, 28 Sep 2002 12:03:23 GMT
Local: Sat, Sep 28 2002 8:03 am
Subject: Re: Simple newbie list processing troubble

Peder Y wrote:
> For example, say, the function shall set all elements of lst to 'A:
> Why is this wrong?

> (defun foo (lst)
>   (loop for x in lst do
>                   (setf (car x) 'A)))

This loops over the contents of the list, not over the
cons cells of the list.   X takes on the values A, B, etc.
which are not cons cells.

To get the effect you want we can change one character:

(defun foo (lst)
  (loop for x on lst do
                  (setf (car x) 'A)))

Here, X takes on the values (A B C D ...), (B C D ...), etc.

Btw, use of the name 'lst' marks you as a Schemer. :)

        Paul


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bruce Hoult  
View profile  
 More options Sep 28 2002, 9:55 am
Newsgroups: comp.lang.lisp
From: Bruce Hoult <br...@hoult.org>
Date: Sun, 29 Sep 2002 01:55:23 +1200
Local: Sat, Sep 28 2002 9:55 am
Subject: Re: Simple newbie list processing troubble
In article <3D959BFC.D1305...@dls.net>, "Paul F. Dietz" <di...@dls.net>
wrote:

> Btw, use of the name 'lst' marks you as a Schemer. :)

Paul Graham uses "lst" throughout _On Lisp_, from the tutorial section
to the mini-CLOS implementation at the end.

-- Bruce


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Sep 28 2002, 10:05 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.no>
Date: 28 Sep 2002 14:05:58 +0000
Local: Sat, Sep 28 2002 10:05 am
Subject: Re: Simple newbie list processing troubble
* Paul F. Dietz
| Btw, use of the name 'lst' marks you as a Schemer. :)

* Bruce Hoult
| Paul Graham uses "lst" throughout _On Lisp_, from the tutorial section to
| the mini-CLOS implementation at the end.

  I read this as affirming Paul's point.  But why bother affirming it, so I
  think perhaps you meant to contradict it.  Would you care to explain why you
  think «On Lisp» is not an effort to bring some of Scheme into Common Lisp?
  (Please note that he has said so himself.)

--
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bruce Hoult  
View profile  
 More options Sep 28 2002, 10:32 am
Newsgroups: comp.lang.lisp
From: Bruce Hoult <br...@hoult.org>
Date: Sun, 29 Sep 2002 02:32:46 +1200
Local: Sat, Sep 28 2002 10:32 am
Subject: Re: Simple newbie list processing troubble
In article <3242210758749...@naggum.no>, Erik Naggum <e...@naggum.no>
wrote:

> * Paul F. Dietz
> | Btw, use of the name 'lst' marks you as a Schemer. :)

> * Bruce Hoult
> | Paul Graham uses "lst" throughout _On Lisp_, from the tutorial section to
> | the mini-CLOS implementation at the end.

>   I read this as affirming Paul's point.  But why bother affirming it, so I
>   think perhaps you meant to contradict it.  Would you care to explain why you
>   think «On Lisp» is not an effort to bring some of Scheme into Common Lisp?
>   (Please note that he has said so himself.)

He has?  Where?

Certainly, the chapter on getting many of the effects of call/cc in CL
(by defining macros such as =defun, =values, etc that manipulate
explicitly-passed continuations) would qualify as an effort to bring
*some* of Scheme into Common Lisp.

On the other hand, it's also an illustration that you don't necessarily
need those features built in if you can implement them yourself using a
sufficiently powerful macro system.

In fact that appears to be the point of the whole book.  That CL's
powerful macros are the prime thing that distinguishes it from lesser
languages, such as Scheme.

Is that the sort of thing a "Schemer" would do?

-- Bruce


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Antoniotti  
View profile  
 More options Sep 28 2002, 1:09 pm
Newsgroups: comp.lang.lisp
From: Marco Antoniotti <marc...@cs.nyu.edu>
Date: 28 Sep 2002 13:10:33 -0400
Local: Sat, Sep 28 2002 1:10 pm
Subject: Re: Simple newbie list processing troubble

pyda...@ec.auckland.ac.nz (Peder Y) writes:
> Say I have a function foo that is supposed to take a list as input (A
> B C D...). The function will then traverse the list looking for an
> element, and change that element. That's basically it.

> For example, say, the function shall set all elements of lst to 'A:
> Why is this wrong?

> (defun foo (lst)
>   (loop for x in lst do
>              (setf (car x) 'A)))

The above will not necessarily work because you are changing the list
as you are traversing it.  This is specifically mentioned in the
language standard.

To do that kind of things you should look to the standard functions
SUBSTITUTE and NSUBSTITUTE.

Cheers

--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
715 Broadway 10th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Sep 28 2002, 2:40 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.no>
Date: 28 Sep 2002 18:40:34 +0000
Local: Sat, Sep 28 2002 2:40 pm
Subject: Re: Simple newbie list processing troubble
* Bruce Hoult <br...@hoult.org>
| Is that the sort of thing a "Schemer" would do?

  It is the sort of thing that somebody who likes Scheme better than Common
  Lisp would do.

--
Erik Naggum, Oslo, Norway                 Today, the sum total of the money I
                                          would retain from the offers in the
more than 7500 Nigerian 419 scam letters received in the past 33 months would
have exceeded USD 100,000,000,000.  You can stop sending me more offers, now.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bruce Hoult  
View profile  
 More options Sep 28 2002, 8:19 pm
Newsgroups: comp.lang.lisp
From: Bruce Hoult <br...@hoult.org>
Date: Sun, 29 Sep 2002 12:19:51 +1200
Local: Sat, Sep 28 2002 8:19 pm
Subject: Re: Simple newbie list processing troubble
In article <3242227234320...@naggum.no>, Erik Naggum <e...@naggum.no>
wrote:

> * Bruce Hoult <br...@hoult.org>
> | Is that the sort of thing a "Schemer" would do?

>   It is the sort of thing that somebody who likes Scheme better than Common
>   Lisp would do.

Seems to me that person would actually write a book called _On Scheme_,
which showed how to implement features they liked from Common Lisp and
Prolog on top of Scheme.

-- Bruce


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Sep 30 2002, 11:31 am
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Mon, 30 Sep 2002 15:31:08 GMT
Local: Mon, Sep 30 2002 11:31 am
Subject: Re: Simple newbie list processing troubble
In article <y6cvg4qt4za....@octagon.valis.nyu.edu>,
Marco Antoniotti  <marc...@cs.nyu.edu> wrote:

I haven't checked the reference, but I'm pretty sure this is a case that's
specifically *allowed* by the standard.  When traversing a list, it's OK to
modify the CAR of the current element, but not the CDRs.

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Hudson  
View profile  
 More options Sep 30 2002, 11:45 am
Newsgroups: comp.lang.lisp
From: Michael Hudson <m...@python.net>
Date: Mon, 30 Sep 2002 15:39:28 GMT
Local: Mon, Sep 30 2002 11:39 am
Subject: Re: Simple newbie list processing troubble

As posted, it's just bust.  If it were (loop for x ON lst do ...),
then I think it's OK by the standard.  Section 3.6 ("Traversal Rules
and Side Effects") says:

  List traversal
    For list traversal operations, the cdr chain of the list is not
    allowed to be destructively modified.

Which suggests to me that mutating the CAR is OK.

Cheers,
M.

--
  The Programmer's Quick Guide To Python (Time Machine version):
    You try to shoot yourself in the foot, only to realize that
    there's no need, since Guido thoughtfully shot you in the foot
    years ago.                     -- Nick Mathewson, comp.lang.python


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Antoniotti  
View profile  
 More options Sep 30 2002, 12:00 pm
Newsgroups: comp.lang.lisp
From: Marco Antoniotti <marc...@cs.nyu.edu>
Date: 30 Sep 2002 12:01:36 -0400
Local: Mon, Sep 30 2002 12:01 pm
Subject: Re: Simple newbie list processing troubble

Thanks to you and Barry.  It does look like changing the CAR of a list
while traversing is ok.

Cheers

--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
715 Broadway 10th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Sep 30 2002, 2:50 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.no>
Date: 30 Sep 2002 18:50:57 +0000
Local: Mon, Sep 30 2002 2:50 pm
Subject: Re: Simple newbie list processing troubble
* Peder Y
| (defun foo (lst)
|   (loop for x in lst do
|               (setf (car x) 'A)))

* Marco Antoniotti
| The above will not necessarily work because you are changing the list as you
| are traversing it.  This is specifically mentioned in the language standard.

  Huh?  The only way the above code would work is by changing the car of each
  cons cell that is the car of the cons cells that are traversed.  This is not
  even close to the restriction of modifying the list structure itself, which
  also means the cdr.  But even (cdr x) would be just fine, because it would
  only modify the conses that were the elements of the list.

  In particular, after (foo bar), bar would have a contents like
  ((A ...) (A ...) (A ...) ...).

--
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Antoniotti  
View profile  
 More options Sep 30 2002, 4:12 pm
Newsgroups: comp.lang.lisp
From: Marco Antoniotti <marc...@cs.nyu.edu>
Date: 30 Sep 2002 16:13:31 -0400
Local: Mon, Sep 30 2002 4:13 pm
Subject: Re: Simple newbie list processing troubble

Yep.  My mistake.

Cheers

--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
715 Broadway 10th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dorai Sitaram  
View profile  
 More options Oct 1 2002, 4:39 pm
Newsgroups: comp.lang.lisp
From: d...@goldshoe.gte.com (Dorai Sitaram)
Date: 1 Oct 2002 20:39:17 GMT
Local: Tues, Oct 1 2002 4:39 pm
Subject: Re: Simple newbie list processing troubble
In article <bruce-C9E44F.01552229092...@copper.ipg.tsnz.net>,
Bruce Hoult  <br...@hoult.org> wrote:

>In article <3D959BFC.D1305...@dls.net>, "Paul F. Dietz" <di...@dls.net>
>wrote:

>> Btw, use of the name 'lst' marks you as a Schemer. :)

>Paul Graham uses "lst" throughout _On Lisp_, from the tutorial section
>to the mini-CLOS implementation at the end.

Paul Graham's use of "lst" is probably because he wants
to keep his code usable, with minimal or automatable
translation, in Scheme also.  Which would help those
who are using his book privately to learn Lisp
principles but trying out his examples in Scheme.  I've
seen this kind of honorable eclecticism in other books
too, notably the Little Schemer, where footnotes
quietly identify how to run the examples in
Common Lisp.

Are there any CL tutorial books that actually advise
using the same symbol for both its symbol-value and its
symbol-function (holding different values)?  I have
seen at least one -- I think it was the Winston and
Horn -- that counseled against this practice
because it is "confusing"!!!   Perhaps they have the
same rationale as Graham, because in the streams (as in
delayed lists, not ports) chapter, they explicitly
mention, somewhat ruefully, that Scheme streams would
have a cleaner syntax.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Oct 1 2002, 6:01 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.no>
Date: 01 Oct 2002 22:01:31 +0000
Local: Tues, Oct 1 2002 6:01 pm
Subject: Re: Simple newbie list processing troubble
* Dorai Sitaram
| Are there any CL tutorial books that actually advise using the same
| symbol for both its symbol-value and its symbol-function (holding
| different values)?

  But that is not what anyone argues for.  Lexical bindings do not affect
  the symbol-value of a the symbol used.  Some precision here is good.

--
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carl Shapiro  
View profile  
 More options Oct 1 2002, 9:10 pm
Newsgroups: comp.lang.lisp
From: Carl Shapiro <cshapiro+s...@panix.com>
Date: 01 Oct 2002 21:10:06 -0400
Local: Tues, Oct 1 2002 9:10 pm
Subject: Re: Simple newbie list processing troubble

d...@goldshoe.gte.com (Dorai Sitaram) writes:

                                               I have

> seen at least one -- I think it was the Winston and
> Horn -- that counseled against this practice
> because it is "confusing"!!!  

More timeless advice from the people who'd like to you belive that use
of CAR and CDR has largely disappeared in favor of FIRST and REST.

Yawn.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Warner  
View profile  
 More options Oct 2 2002, 12:54 am
Newsgroups: comp.lang.lisp
From: "Adam Warner" <use...@consulting.net.nz>
Date: Wed, 02 Oct 2002 16:54:17 +1200
Local: Wed, Oct 2 2002 12:54 am
Subject: Re: Simple newbie list processing troubble
Hi Dorai Sitaram,

I love the freedom of separate namespaces. But perhaps I can receive some
help on a related question.

I am updating syntax and function names on a regular basis in the document
format I'm developing. So that legacy documents don't have to be manually
updated I upgrade them automatically. I love the way I can read in lists
of Lisp code as data and process them without operating on the code at the
character level (and I set *print-case* to :downcase so the updated code
doesn't start shouting).

But how should I proceed with automatically updating code when a
variable/symbol name may be the same as a function name and I only want to
update the function name (or vice versa)? Is there any built-in way to
find out whether something is referring to a function/macro or do I have
to parse the syntax manually (for example noting that a particular list is
quoted and therefore the symbol at the start of a list doesn't refer to a
function).

In Richard C. Waters Macroexpand-All: An Example of a Simple Lisp Code
Walker http://www.merl.com/papers/TR93-17/ it states that there are 25
special forms and each has its own special semantics. It seems that if I
want to be able to robustly update code that I have to understand many of
these special forms so I do not accidentally modify a variable name
instead of a non-function name with the same name, etc. (it's similar to
the list/list issue).

Maybe a simple example could focus the question:

(let ((list (list '(list 'list))))
  (write (list list))
  (write '(list list)))

What would be the robust way of processing this code so that only the
function name list is changed to newlist? Some of the difficulty in
parsing this code arises out of the separate namespaces.

Regards,
Adam


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Coby Beck  
View profile  
 More options Oct 2 2002, 1:44 am
Newsgroups: comp.lang.lisp
From: "Coby Beck" <cb...@mercury.bc.ca>
Date: Wed, 2 Oct 2002 15:45:19 +1000
Local: Wed, Oct 2 2002 1:45 am
Subject: Re: Simple newbie list processing troubble

"Adam Warner" <use...@consulting.net.nz> wrote in message

news:andu5q$dgso2$1@ID-105510.news.dfncis.de...

> I am updating syntax and function names on a regular basis in the document
> format I'm developing. So that legacy documents don't have to be manually
> updated I upgrade them automatically. I love the way I can read in lists
> of Lisp code as data and process them without operating on the code at the
> character level (and I set *print-case* to :downcase so the updated code
> doesn't start shouting).

> But how should I proceed with automatically updating code when a
> variable/symbol name may be the same as a function name and I only want to
> update the function name (or vice versa)? Is there any built-in way to

[snip]

> Maybe a simple example could focus the question:

> (let ((list (list '(list 'list))))
>   (write (list list))
>   (write '(list list)))

> What would be the robust way of processing this code so that only the
> function name list is changed to newlist? Some of the difficulty in
> parsing this code arises out of the separate namespaces.

While I always hesitate to say anything is impossible, I don't see how this
could be.  Even the almighty human brain can not look at that and make the
substitution without knowing what the semantics of your application are.
How can I know that '(list list) or 'list is data, not code?  Being quoted
does not prove that, as you said code is data in lisp.  What about (mapcar
'list ...)?

I think the best you could do would be a naive approach that assumed no code
is being passed around as data and no functions are passed symbols instead
of function objects (ie #'list), then you could globally replace all
occurences of <(target > and <#'target > with many messy exceptions (like
LET ...)

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Bradshaw  
View profile  
 More options Oct 2 2002, 3:35 am
Newsgroups: comp.lang.lisp
From: Tim Bradshaw <t...@cley.com>
Date: 02 Oct 2002 08:11:10 +0100
Local: Wed, Oct 2 2002 3:11 am
Subject: Re: Simple newbie list processing troubble

* Adam Warner wrote:
> Maybe a simple example could focus the question:
> (let ((list (list '(list 'list))))
>   (write (list list))
>   (write '(list list)))
> What would be the robust way of processing this code so that only the
> function name list is changed to newlist? Some of the difficulty in
> parsing this code arises out of the separate namespaces.

You need a code walker.  This has to understand all the special forms
defined by the language, as well as being able to do full
macroexpansion.  For instance, it needs to know that if I've said:

    (defmacro bind ((var val) &body forms)
      `(let ((,var ,val)) ,@forms))

    (bind (list 1)
      list)

That the occurrence of LIST isn't a function call.  The way to know
this is to do macroexpansion.

Note that the code walker needs to take note of macros you define as
well as CLs.  Partly this can be done by just assuming that you've
already loaded your system, but even this is not quite enough:

    (macrolet ((bind ((var val)) &body forms)
                 `(let ((,var ,val)) ,@forms))
      (bind (list 1)
        list))

It's quite hard to get this completely right.

--tim


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Warner  
View profile  
 More options Oct 2 2002, 4:00 am
Newsgroups: comp.lang.lisp
From: "Adam Warner" <use...@consulting.net.nz>
Date: Wed, 02 Oct 2002 20:00:56 +1200
Local: Wed, Oct 2 2002 4:00 am
Subject: Re: Simple newbie list processing troubble
Hi Coby Beck,

Thanks for the insights Coby. I would hesitate to say it is impossible
because it must be possible to trace back from the function calls to what
generated them. But I see now that it would require something many times
more complicated and intelligent than the original interpreter/compiler.
To give another example from your insights, what sort of AI would be able
to deduce that this code:

(funcall (read-from-string (coerce (list (code-char 76) (code-char 73)
(code-char 83) (code-char 84)) 'string)) 'list)

...(using ASCII/UTF-8 character codes) should be changed to:

(newlist 'list)

[To change all list function calls to newlist in the contrived example]

Naive approach methinks (or explicit version control).

Regards,
Adam


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Warner  
View profile  
 More options Oct 2 2002, 4:08 am
Newsgroups: comp.lang.lisp
From: "Adam Warner" <use...@consulting.net.nz>
Date: Wed, 02 Oct 2002 20:08:50 +1200
Local: Wed, Oct 2 2002 4:08 am
Subject: Re: Simple newbie list processing troubble
Hi Tim Bradshaw,

Thanks for how this would be approached Tim. Analysing Richard Waters'
macroexpand-all code would be a good start.

Given the example I just posted it would be impractical to get it
completely right.

Regards,
Adam


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Marsden  
View profile  
 More options Oct 2 2002, 5:45 am
Newsgroups: comp.lang.lisp
From: Eric Marsden <emars...@laas.fr>
Date: Wed, 02 Oct 2002 11:35:58 +0200
Local: Wed, Oct 2 2002 5:35 am
Subject: Re: Simple newbie list processing troubble

>>>>> "aw" == Adam Warner <use...@consulting.net.nz> writes:

  aw> Maybe a simple example could focus the question:
  aw>
  aw> (let ((list (list '(list 'list))))
  aw>    (write (list list))
  aw>    (write '(list list)))
  aw>
  aw> What would be the robust way of processing this code so that only the
  aw> function name list is changed to newlist? Some of the difficulty in
  aw> parsing this code arises out of the separate namespaces.

Here is how to do this using the PCL code walker (available with
CMUCL). The walker allows you to distinguish between uses of the
symbol list as a function and as data, including Tim's example of a
macro that changes the symbol binding. However, it won't detect uses
of the list function via FUNCALL; that requires whole program analysis
in the general case.

,----
| USER> (defvar *walker-demo-form*
|         (read-from-string
|          "(let ((list (list 'list '(list 'list))))
|              (write (list list))
|              (write (funcall 'list list))
|              (write '(list list)))"))
| *walker-demo-form*
| USER> (defun walker-demo (form)
|         (flet ((walk-function (form context env)
|                  (declare (ignorable env))
|                  (cond ((not (eq context :eval)) form)
|                        ((not (listp form)) form)
|                        ((and (symbolp (first form))
|                              (fboundp (first form))
|                              (eq 'list (first form)))
|                         (cons 'newlist (rest form)))
|                        (t form))))
|           (walker:walk-form form nil #'walk-function)))
| walker-demo
| USER> (walker-demo *walker-demo-form*)
| (let ((list (newlist 'list '(list 'list))))
|   (write (newlist list))
|   (write (funcall 'list list))
|   (write '(list list)))
| USER> (defmacro bind ((var val) &body forms)
|         `(let ((,var ,val)) ,@forms))
| bind
| USER> (walker-demo '(bind (list 1) list))
| (bind (list 1) list)
`----

--
Eric Marsden                          <URL:http://www.laas.fr/~emarsden/>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alan S. Crowe  
View profile  
 More options Oct 2 2002, 6:39 am
Newsgroups: comp.lang.lisp
From: a...@cawtech.freeserve.co.uk (Alan S. Crowe)
Date: 02 Oct 2002 11:42:57 +0100
Local: Wed, Oct 2 2002 6:42 am
Subject: Re: Simple newbie list processing troubble
To: Erik Naggum <e...@naggum.no>
Subject: Re: Simple newbie list processing troubble
References: <10b28c6c.0209280104.52056c24@posting.google.com> <3D959BFC.D1305E7D@dls.net> <bruce-C9E44F.01552229092002@copper.ipg.tsnz.net> <and15l$bjp$1@news.gte.com> <3242498491969809@naggum.no>
--text follows this line--

Erik Naggum writes:
> Lexical bindings do not affect the symbol-value of a the
> symbol used.

* (defvar x 137)
X

* x
137

* (let ((x 'dragon))
    (list
     x
     (symbol-value 'x)))

(DRAGON DRAGON)

err, um, I expected (DRAGON 137)

* x
137

Well my old value of x is still there, so a symbol must have
multiple "symbol values", even when it is special. This
needs more investigation.

Alan Crowe, Edinburgh, Scotland


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frode Vatvedt Fjeld  
View profile  
 More options Oct 2 2002, 7:00 am
Newsgroups: comp.lang.lisp
From: Frode Vatvedt Fjeld <fro...@cs.uit.no>
Date: Wed, 02 Oct 2002 13:00:37 +0200
Local: Wed, Oct 2 2002 7:00 am
Subject: Re: Simple newbie list processing troubble
a...@cawtech.freeserve.co.uk (Alan S. Crowe) writes:

> err, um, I expected (DRAGON 137)

What (defvar <x> ..) does, among other things, is to proclaim <x> to
be a special variable. One consequence of this is that it will be
_impossible_ to create a lexical binding for <x>. So when (let ((<x>
..)) is later evaluated or compiled, a dynamic binding is created.

> Well my old value of x is still there, so a symbol must have
> multiple "symbol values", even when it is special.

Each symbol has as many symbol-values as it has bindings in a
particular (dynamic) scope. But only the last/innermost binding's
value is available to you.

--
Frode Vatvedt Fjeld


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 36   Newer >
« Back to Discussions « Newer topic     Older topic »