find_root and variable capture?



On Fri, Mar 7, 2008 at 4:52 PM, Zach <elzacho at gmail.com> wrote:
> I was unable to find any posts on quote or quoting in the maxima list
> archives.  I'm sure they are there, but when I type in quote or quoting in
> the little google search box I get nothing back.

Where is this "little google search box"? In my own mail archives, I
find messages about quoting on 12/26/07, 11/06/07, 1/19/07, just to
start.

>  It might be really helpful...to have a mailing list FAQ.

Well, there's http://maxima.sourceforge.net/wiki/index.php/Maxima%20FAQ,
most of the content of which comes from the mailing list.

> > If you don't quote the arguments, they will be evaluated when you
>  > *call* find_root, not when find_root calls them....

> Okay, I do not understand this...  I guess I don't have a strong
> understanding (scratch that, any understanding) of the maxima evaluation
> procedure.  In the Lisp evaluator (if we ignore special forms and macros),
> it goes arguments left to right then call the function with those arguments.
> I am not sure when find_root calls an argument.

This should be easy to understand if you understand Lisp.  In Lisp
terms, you want to write (find_root '(+ x 3) ...), not (find_root (+ x
3) ...). The big difference is that in Maxima, it is *possible* to
write (+ x 3) which, if x is not defined, evaluates to (+ x 3).  A
cleaner way of doing this would make the bound variable explicit, e.g.
(find_root (lambda (x) (+ x 3)) ...), but Maxima isn't very clever
about lambda.

> It looks like quoting is not always the right thing to do, like if I want to write a wrapper for find_root.

Exactly.

> f(y) := find_root('(exp(-x)-y), 'x, 0, 100);
> f(.1); => some correct number
> f(y); => find_root(exp(-x)-y, x, 0, 100) (as expected)

find_root is trying to be clever here and act like a simplifying
function (the same way that x+3 evals to x+3 when x isn't defined),
but since it is not a mathematical function, this won't work.

> Am I to read from this that maxima doesn't use lexical scoping?

Correct.

> It seems that the block facility allows for something like lexical scoping.

No, block is a *syntax* which doesn't specify either lexical or
dynamic scoping.  As it happens, Maxima (inheriting from old Lisp
semantics) uses dynamic scoping.  There have been many discussions
about changing that, but in a system like Maxima where x is sometimes
equivalent to 'x, it is not clear what lexical scoping should do in
some cases....

> However, without the capability of generic symbols, I suppose one is forced to use
> obscure names to reduce the chances of name collisions.

With true lexical scoping, you don't need obscure names.  It's because
it's using dynamic scoping that you need the obscure-name trick. By
"generic symbols", I guess you mean "generated symbols" (gensyms)?
Those are needed for macros, which aren't the issue here.

         -s