Hi Here are a few questions:
I am still having trouble controlling when expressions are
evaluated. I wonder if anyone has some tricks.
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 ?
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.
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.
But this does not work.
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.
I try inserting quote marks and double quote marks in various places
in the definition and when calling the function, but nothing works.
It is possible to do it in lisp. The create_list function itself can handle
expressions such as part(m,i). I am getting better at lisp and can probably
do it soon. But it is frustrating not knowing how to do these things in
macsyma.
3) This is a different question.
Someone said before (Robert D. or Stavros M. ?) that they thought this
was more or less the only way to do the following:
g(x,[args]) := block(...
f([args]) := apply( 'g, cons(some_expr, [args]))
In Mathematica, multiple arguments are done with patterns and the previous
function would look like this:
f([args]) := g(some_expr,args);
because the args are spliced in. If you want them in list
form, you can make a list of them explicitly. This seems
like a better way for other reasons as well. For
instance, it is easy to translate a macsyma function
definition with variable args to a Mma definition (eg
just insert 'newargs = List[args]' in the variable
declaration block in the function definition). But it is
in general very difficult to go the other way.
It may be difficult to add such a facility to macsyma. It may be the best
way is
introduce a pattern matching system as Richard F. suggested. I don't know,
I
am just suggesting that it is a useful feature.
4) Finally. I want to write something to do a general transpose of a nested
list.
Eg, transposing a list with two levels means sending levels [1,2] to [2,1].
With
a three-level list, you could, for example, reorder the levels like this
[1,2,3] -> [3,1,2], etc. I am thinking that since any permutation of a list
of objects can be decomposed into a series of permutations at two
neighboring
positions, the tranposition can be accomplished using this decomposition
combined
with ordinary transposition. But this might not be efficient. I wonder if
anyone
has given this thought before.
Thanks,
John