I want to define a function that only operates on expressions with a head from a given list, such as
f[expr:(_And|_Or)] := Print["LHS is ", expr[[1]]];
This of course works fine when x
is just an expression such as expr = p == q || p == q + 1
. However, now I'd like to be able to print out parts of expr
even when p and q both have values defined. That is, if f
and expr
are defined as above, then
p = 1;
q = 2;
f[expr]
should print "LHS is True". The obvious thing to try is of course to add
SetAttributes[f, HoldFirst]
but now f
doesn't match expr
, i.e. f[expr]
simply returns f[expr]
. I've tried variations of this, but nothing I can come up with seems to work, expr
is always either evaluated before matching the head or the correct head is not found.