Newbie Question: Are arguments in a function definition bound when function is called?
Subject: Newbie Question: Are arguments in a function definition bound when function is called?
From: Stavros Macrakis
Date: Wed, 26 Aug 2009 08:29:50 -0400
Maxima has separate 'function' and 'value' slots for symbols. (Lisp-2 semantics)
Assignment (q: ...) and binding set the value slot; function
definition (q(x) := ...) sets the function slot. When evaluating the
function call q(...), Maxima first checks the function slot, and only
if there is no function value does it check the value slot. Google
[lisp-1 lisp-2] for discussion. It is actually messier than that in
Maxima, because a defined function may also have a simplification
property, so '(f(x)) may simplify even though f is never called....
But that and the related noun/verb scheme are a discussion for another
day.
To call the _value_ of q, use funcall(q,...) or (q)(...).
-s
On 8/26/09, Josephy, Norman <NJOSEPHY at bentley.edu> wrote:
> Hi:
>
> I am confused about the following behaviour and would appreciate if someone
> could explain it to me.
>
> I am using Maxima 5.19.1 and the bundled wxMaxima 0.8.3a under
> Microsoft Windows XP x64 edition Version 2003 Service Pack 2.
>
> I am going to have my students use Maxima for the first time in my calculus
> class.
>
> I wish to avoid situations where the mechanism for binding an argument in
> a function definition does not bind to the value passed to it from a
> function call.
>
> kill(all);
>
> /* Define a function that is passed a function name */
> g(f,x) := f(x);
>
> /* Test the function definition */
> g(sin,5); /* --> sin(5) o.k. */
>
> /* Bind the name used in the function definition to a specific function */
> f : lambda([x],x^2);
>
> /* Verify that the function g still 'works' as before */
> g(f,x); /* --> x^2 o.k. */
> g(sin,5); /* --> sin(5) o.k. */
>
> /* Repeat the above using the alternative notation for function definition
> */
> kill(all);
> g(f,x) := f(x);
> f(x) := x^2;
> g(f,x); /* --> x^2 o.k. */
> g(sin,5); /* --> 25 ??? */
>
>
>
> It seems that the argument f in the definition of g gets bound to
> the definition x^2.
>
> I was 'hoping' that the arguments in a function definition would
> always get bound to whatever arguments are passed to it from the calling
> function.
>
> Thank you in advance.
>
> Norman
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>