Re: How do you do this?
- From: noisesmith@xxxxxxxxx
- Date: Mon, 28 Apr 2008 22:09:15 -0700 (PDT)
On Apr 21, 11:50 am, rocco.ro...@xxxxxxxxx wrote:
On Apr 21, 7:55 pm, Ron Garret <rNOSPA...@xxxxxxxxxxx> wrote:
In article
<5625df53-ddb6-40a5-bb4d-e67695a0b...@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
rocco.ro...@xxxxxxxxx wrote:
Please have a look at the following Python program. It is totally
useless, but I wrote it to illustrate something that puzzles me:
def fn(name):
x = y = z = 5
stmnt = name + " = " + "10"
exec(stmnt)
return x + 2 * y + 3 * z
while True:
name = raw_input("Enter variable to be set to 10 (x, y, or z). q
to quit: ")
if not name in ("x", "y", "z", "q"):
print "I said: x, y, z, or q!"
continue
if name == "q":
break
print "Result: " + str(fn(name))
Pay attention to the fn function. Through the use of the "exec"
statement, Python can execute statements (in this case assign values)
in the lexical scope where "exec" is located. I believe you can even
pass parameters such as globals() or locals() to fine-tune that
behavior.
So, in this case, I was capable of choosing, at runtime, the variable
(x, y, or z) which was to be assigned the value 10, while leaving the
other two at 5, and consequently (but not important) getting a
different return value in each case.
Now, from what I understand, we can't do this with EVAL, because it
won't "see" the variables in the lexical scope in which it is located,
in contrast with Python's exec.
And I see no way we can achieve the same result with macros either.
Never mind the total uselesness of the program per se. What I'm trying
to say is that it bothers me that such a beautiful and powerful
language as Scheme apparently won't le me do something that appears to
be relatively trivial like this (for instance, using strings to to do
operations at runtime in any lexical scope.)
Am I missing something? (I really hope so!)
Thanks.
You are missing the fact that Python is designed to be an interpreted
language while Scheme is designed to be (at least potentially) compiled.
When lexical environments are compiled it is very common to optimize the
code by "compiling away" the variable names. This is possible because
lexical bindings are always resolvable at compile time. (This is true
by definition. If a binding is not resolvable at compile time then it
is not a lexical binding.)
Because Python is designed to be interpreted, the names of lexical
variables stick around at run time. This can be convenient, but the
tradeoff is that it makes Python that much harder to compile
efficiently. Scheme makes the opposite tradeoff, sacrificing some
convenience for easier efficient compilation.
rg
Yes, thank you very much for the clarification. I hadn't thought about
the implications of Scheme (and Lisp in general) being always
potentially compilable. It's very clear now. It's mainly a matter of
an efficiency tradeoff and much less, I believe, a matter of being
able to prove certain properties about programs, because:
1. like I said before, Scheme is really not a pure functional language
(such as Haskell), and it isn't even statically typed like ML, so I
don't think it's this remarkable champion of theorem proving
languages. And, by the way, I've read somewhere that apparently even
when you *are* working with one of those powerful systems, there is
quite a limit on the things you can *practically* prove anyway.
2. Common Lisp is designed just the same in this respect, and (as
powerful as it is - I just love it) I've never heard anyone pretend
that CL was made especially to prove properties about programs (even
though I'm not saying that it can't be done, I suppose).
So I guess you really said it all. Thanks again.
The question is not proving EVERYTHING. The issue is: if you can know
without a doubt that this set of variables will a: never change type
and b: never be accessed by any code that would need to check their
type, then you can get a huge performance boost (think 10 - 100 times
faster for some code). Lexical closures allow for that. Passing code
into a closure that can access its internals means you no longer have
such thing as a closure. The more you know about a program at compile
time, the more the code can be streamlined. If you can handle the
performance hit, make a macro that grabs values from a hash table at
run time. The code does not have to look any different from normal
scheme code, if you use hygienic macros. Or maybe this means that
scheme is the wrong match for what you are trying to do, and you need
a scripting language. In my humble opinion, one gains more from
thinking in scheme while using python, than from thinking in python
while using scheme.
.
- References:
- How do you do this?
- From: rocco . rossi
- Re: How do you do this?
- From: Ron Garret
- Re: How do you do this?
- From: rocco . rossi
- How do you do this?
- Prev by Date: Wraith Scheme 1.32 released (for Macintosh); does parallel processing
- Next by Date: Re: ANN: irregex - portable and efficient regular expressions for scheme
- Previous by thread: Re: How do you do this?
- Next by thread: Re: How do you do this?
- Index(es):
Relevant Pages
|