evaluation



On Thu, Oct 9, 2008 at 2:41 AM, John Lapeyre <john at johnlapeyre.com> wrote:

> 1) For instance, suppose I wanted to write a 'for' function
>  rather than use the for statement. eg
>  for(test,true_clause,false_clause);
>  ( you could also have an 'unknown' clause)
>  Of course, you only want to evaluate one of the second and third arguments,
>  depending on the first argument. Is there a way to do this ?

I'm not sure what you want, but it seems likely you want this
custom loop to be a macro function (defined by ::=).

If you are willing to do Lisp programming, you could make the function
a defmspec function.

> 2) Here is a sort of artificial example.
>
>   create_list(i,i,1,3) --> [1, 2, 3]
>
>  f(expr) := create_list(expr,i,1,3);
>
>   f(i) --> [i, i, i]
>
>  Thats not what I want, but I am not sure why it happens.

Maxima has 1-time evaluation. create_list evaluates expr with i bound
to the values 1, 2, 3. expr is bound to the symbol i so in each case,
create_list evaluates expr to i (which is not re-evaluated).

Incidentally the ev function recognizes a flag infeval which causes
expressions to be evaluated until they stop changing.

>  f(expr) := apply('create_list, [expr,i,1,3]);
>
>   f(i) --> [1, 2, 3]
>
>  That does work. The documentation for 'apply' says that apply
>  evaluates its arguments even if the function being applied quotes
>  them. Maybe that is the reason for the difference.

Yes.

>  f(expr) := apply('create_list, [expr,i,1,3]);
>  m : [1,2,3]
>
>  f(m[i]) and f(part(m,i)) both give
>
>  Subscript must be an integer:
>  i
>   -- an error.
>
>  This is apparantly because 'expr' is being evaluated once before it
>  is evaluated with integers substituted for 'i'. Eg
>  part(m,j) gives the same error if j evaluates to itself.

The subscript of a literal list must be a literal integer. I think that's less
useful than allowing a literal list to have a symbolic subscript. I've
tinkered with code to change it but didn't finish it. I think I've floated
the idea on this list before; I don't remember what came of it.

I'm pretty sure create_list doesn't come into play in this case.

>   In Mathematica, multiple arguments are done with patterns and the previous
>   function would look like this:
>
>   f([args]) := g(some_expr,args);

I'm opposed to making Maxima splice in lists automatically.
It seems like a minor convenience which makes Maxima's behavior
inconsistent and therefore unnecessarily more difficult to explain.

> 4) Finally. I want to write something to do a general transpose of a nested list.

If the goal is to transpose a matrix which is represented by a nested list,
then my advice is just work with a matrix.

Hope this helps

Robert Dodier