Message Boards Message Boards

4 Replies
3 Total Likes
View groups...
Share this post:

Is there any equivalent to macroexpand-1 in WL?

Sorry for the LISP reference, but it's probably the best example.

For example, if I write:

Commute[Times[x_,y_]] := Times[y_,x_]

And I want to test this on Times[1,2], is there any input I can give to get the response Times[2,1]?

If I say Commute[Times[1,2]] then 1*2 is evaluated first and Commute does not run because the pattern does not match.

If I say Commute[Unevaluated[Times[1,2]] then Commute runs properly but Times[2,1] is evaluated inside Commute and 2 is returned.

If I say Unevaluated[Commute[Times[1,2]]] then Commute is not evaluated and I just get the same expression back again.

I know I can test it by using unassigned variables, so that the Times expression cannot be evaluated, but is there a way to do so without needing to worry about that?

POSTED BY: Mark Green
4 Replies

It is possible mucking about with holdform could yield something, but I can't work out how I'd go about that I'm afraid. If you get desperate you could overload Commute with specific debug procedures.

POSTED BY: David Gathercole

Commute[Times[x_, y_]] := HoldForm[Times[y, x]]


POSTED BY: David Gathercole

Thanks, but I don't want to change the definition of Commute. That makes it into a function that can only ever return that form, whereas I just want to return the unevaluated form this once for testing/debugging purposes (which is what macroexpand-1 does)

POSTED BY: Mark Green
Reverse[Inactive[Times][1, 2]]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract