[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: can s-exprs represent a graph without eval?
On Thu, 2003-06-19 at 16:07, Guy Steele - Sun Microsystems Labs wrote:
> Date: Thu, 19 Jun 2003 15:30:56 -0400
> Subject: Re: can s-exprs represent a graph without eval?
> From: Geoffrey Knauth <email@example.com>
> To: firstname.lastname@example.org
> I've never seen this, it's certainly interesting, but how to you detect
> that you're just going around in circles?
> (Let's say someone passes you v and you aren't suspecting v has this
> Well, which is it? If you aren't suspecting,
> you aren't going to conduct the test that
> might reveal it.
> Sometimes an circular structure can be used to "advantage".
> Consider this bit of code:
> (mapcar #'+ x '#1=(1 -1))
> If x is (1 4 7 10), the result will be (2 3 8 9).
> Does this puzzle you? You get 1 point.
> Does this delight you? You get 5 points.
> Does this nauseate you? You get 10 points.
Is it possible to get 16 points over time? It puzzled me for a moment,
and delighted me until I considered maintaining some code that used it.
It is a handy notation. In Python, you usually have to construct such
objects by hand. Easy in this specific case, but not in general.
>>> from itertools import imap, cycle
>>> for i in imap(int.__add__, (1, 4, 7, 10), cycle((1, -1))):
... print i
In Python, you need to use imap rather than the builtin map, because the
builtin map runs until the longest sequence is exhausted, filling in
None for the elements of the shorter sequences. A misfeature, but too
late to correct.