I have an inner pattern
f[g, n_] := inner
and I want to define an outer curried pattern
f[h, m_][f[g, n_]] := ...
which has unrelated behaviour to f[g,n]
and so should receive expression f[g,n]
rather than inner
. Alas, f[h,m][f[g,n]]
is undesiredly first evaluated as f[h,m][inner]
. How can I prevent Mathematica from evaluating the inner f[g,n]
whilst still recognising a pattern for f[h,m_][..]
without having to explicitly insert Hold
into the outermost expression?
Of course SetAttribute[f, HoldAll]
doesn't work, and I can't exactly set an attribute for the experssion f[h]
.
My motivation: I'm creating inner "functions" with subscript "arguments", and I want to define an outer "functional" which also has subscript "arguments" which should start evaluating before the passed inner functions are evaluated.
E.g.
Subscript[g1, n_] := bad[n]
Subscript[g2, n_] := bad[n]
...
Subscript[h, m_][ Subscript[g_,n_] ] := good[g,m,n]
I want the behaviourwhere $g1_n$ gives bad[n]
, but $h_m[g1_n]$ gives good[g1,m,n]
. The above code instead gives $h_m[bad[n]]$.
I notice that the last line of the above code doesn't affect the DownValues of Subscript:
DownValues[Subscript]
{HoldPattern[Subscript[g1, n_]] :> bad[n],
HoldPattern[Subscript[g2, n_]] :> bad[n]}
Is this at all possible?
Subscript
while defining symbols (variables) should be avoided.Subscript[x, 1]
is not a symbol, but a composite expression whereSubscript
is an operator without built-in meaning. You expect to do $x_1=2$ but you are actually doingSet[Subscript[x, 1], 2]
which is to assign aDownValues
to the operatorSubscript
and not anOwnValues
to an indexedx
as you may intend. Read how to properly define indexed variables here $\endgroup$/:
to attach anOwnValues
tox
if that was really a problem $\endgroup$