|
From: Robert D. <rob...@gm...> - 2020-07-07 04:53:54
|
On Mon, Jul 6, 2020 at 8:43 PM Eli Damon <pu...@el...> wrote:
> (%i1) sublis([n=3],print("f(n)","=",f(n)));
> f(n) = f(n)
> (%o1) f(3)
Eli, it turns out this is exactly the behavior one should expect.
sublist is an ordinary function, i.e. one which evaluates its
arguments. So print("f(n)","=",f(n)) is evaluated, and it evaluates to
f(n), and has the side effect of printing "f(n) = f(n)". Now sublis is
called with arguments [n = 3] and f(n), so it returns f(3), which you
see as %o1.
I guess that you want to print an expression without evaluating it and
also print it evaluated. The built-in functions display and ldisplay
are a little bit like that, but not exactly. Here is an attempt to do
just that.
mydisplay (e, [vv]) ::= buildq ([e, e1: sublis (vv, e)], print ('e = 'e1));
It's a little delicate to get just the right amount of evaluation ...
note that mydisplay is a macro, defined by ::= , which is a function
which quotes its arguments, and its return value is evaluated by the
caller. Note also the presence of single quotes in print('e = 'e1),
that prevents e and e1 from being evaluated by the caller. Here's what
I get for the above input.
(%i35) mydisplay (f(n), n = 3);
f(n) = f(3)
(%o35) f(n) = f(3)
mydisplay collects any arguments aside from e and puts them all in the list vv.
(%i36) mydisplay (g(m, n), n = a, m = 2*a);
g(m, n) = g(2 a, a)
(%o36) g(m, n) = g(2 a, a)
I don't know whether mydisplay should print e = e1 or just return it.
I guess which one is more useful depends on what you need to do. Maybe
you can say more about what your goal is.
There are probably other variations which are useful in some context.
E.g. f(n) = f(3) = 1234 or whatever depending on the definition of f.
Hope this helps,
Robert Dodier
|