Outputting expressions in the order in which they're entered
Clash Royale CLAN TAG#URR8PPP
Background
For display purposes, I sometimes find it desirable to defeat Mathematica's canonical ordering of variables, and instead have it output terms in the order in which I type them. E.g., instead of this:
b-a
v=v0+a t
a8+a9+a10
–a + b
a t + v0
a10 + a8 + a9
...I'd prefer this:
b – a
v0 + a t
a8 + a9 + a10
This can be easily accomplished by removing the Orderless attributes from Plus and Times, entering the expression, and then immediately restoring those attributes, but I don't know of a way to implement this series of commands as a function:
ClearAttributes[Plus, Orderless]
ClearAttributes[Times, Orderless]
e1=c (b-a)
SetAttributes[Plus, Orderless]
SetAttributes[Times, Orderless]
e2=c (b-a) (*confirm that Orderless is restored to both Plus and Times*)
e1==e2 (*check that functionality isn't affected by how function is displayed; want output to be "True"*)
c (b – a)
(–a + b) c
True
Question
How do I define a function such that:
e1=func[c (b-a)]
e2=c (b-a) (
e1==e2
c (b – a)
(–a + b) c
True
Notes:
(1) I want to alter only the display format, not any other functionality. E.g., MMA should still recognize that e1 and e2 are mathematically equivalent, even though e1 was defined while the Orderless Attribute was absent from both Plus and Times.
(2) This is not quite a duplicate of Changing the display ordering of orderless functions? because there the OP was looking for a function that would dictate the display order, while I was looking for a simpler approach that merely displayed the output in the same order that I typed it. [So the function would just act to protect the display order, rather than specify it.]
(3)This was originally a question that ended up having two separate parts.
To make the information in this more readily searchable, I've divided this question into two (this being the first part). See: Dividing one thread into two
function-construction output-formatting sorting output pure-function
add a comment |
Background
For display purposes, I sometimes find it desirable to defeat Mathematica's canonical ordering of variables, and instead have it output terms in the order in which I type them. E.g., instead of this:
b-a
v=v0+a t
a8+a9+a10
–a + b
a t + v0
a10 + a8 + a9
...I'd prefer this:
b – a
v0 + a t
a8 + a9 + a10
This can be easily accomplished by removing the Orderless attributes from Plus and Times, entering the expression, and then immediately restoring those attributes, but I don't know of a way to implement this series of commands as a function:
ClearAttributes[Plus, Orderless]
ClearAttributes[Times, Orderless]
e1=c (b-a)
SetAttributes[Plus, Orderless]
SetAttributes[Times, Orderless]
e2=c (b-a) (*confirm that Orderless is restored to both Plus and Times*)
e1==e2 (*check that functionality isn't affected by how function is displayed; want output to be "True"*)
c (b – a)
(–a + b) c
True
Question
How do I define a function such that:
e1=func[c (b-a)]
e2=c (b-a) (
e1==e2
c (b – a)
(–a + b) c
True
Notes:
(1) I want to alter only the display format, not any other functionality. E.g., MMA should still recognize that e1 and e2 are mathematically equivalent, even though e1 was defined while the Orderless Attribute was absent from both Plus and Times.
(2) This is not quite a duplicate of Changing the display ordering of orderless functions? because there the OP was looking for a function that would dictate the display order, while I was looking for a simpler approach that merely displayed the output in the same order that I typed it. [So the function would just act to protect the display order, rather than specify it.]
(3)This was originally a question that ended up having two separate parts.
To make the information in this more readily searchable, I've divided this question into two (this being the first part). See: Dividing one thread into two
function-construction output-formatting sorting output pure-function
add a comment |
Background
For display purposes, I sometimes find it desirable to defeat Mathematica's canonical ordering of variables, and instead have it output terms in the order in which I type them. E.g., instead of this:
b-a
v=v0+a t
a8+a9+a10
–a + b
a t + v0
a10 + a8 + a9
...I'd prefer this:
b – a
v0 + a t
a8 + a9 + a10
This can be easily accomplished by removing the Orderless attributes from Plus and Times, entering the expression, and then immediately restoring those attributes, but I don't know of a way to implement this series of commands as a function:
ClearAttributes[Plus, Orderless]
ClearAttributes[Times, Orderless]
e1=c (b-a)
SetAttributes[Plus, Orderless]
SetAttributes[Times, Orderless]
e2=c (b-a) (*confirm that Orderless is restored to both Plus and Times*)
e1==e2 (*check that functionality isn't affected by how function is displayed; want output to be "True"*)
c (b – a)
(–a + b) c
True
Question
How do I define a function such that:
e1=func[c (b-a)]
e2=c (b-a) (
e1==e2
c (b – a)
(–a + b) c
True
Notes:
(1) I want to alter only the display format, not any other functionality. E.g., MMA should still recognize that e1 and e2 are mathematically equivalent, even though e1 was defined while the Orderless Attribute was absent from both Plus and Times.
(2) This is not quite a duplicate of Changing the display ordering of orderless functions? because there the OP was looking for a function that would dictate the display order, while I was looking for a simpler approach that merely displayed the output in the same order that I typed it. [So the function would just act to protect the display order, rather than specify it.]
(3)This was originally a question that ended up having two separate parts.
To make the information in this more readily searchable, I've divided this question into two (this being the first part). See: Dividing one thread into two
function-construction output-formatting sorting output pure-function
Background
For display purposes, I sometimes find it desirable to defeat Mathematica's canonical ordering of variables, and instead have it output terms in the order in which I type them. E.g., instead of this:
b-a
v=v0+a t
a8+a9+a10
–a + b
a t + v0
a10 + a8 + a9
...I'd prefer this:
b – a
v0 + a t
a8 + a9 + a10
This can be easily accomplished by removing the Orderless attributes from Plus and Times, entering the expression, and then immediately restoring those attributes, but I don't know of a way to implement this series of commands as a function:
ClearAttributes[Plus, Orderless]
ClearAttributes[Times, Orderless]
e1=c (b-a)
SetAttributes[Plus, Orderless]
SetAttributes[Times, Orderless]
e2=c (b-a) (*confirm that Orderless is restored to both Plus and Times*)
e1==e2 (*check that functionality isn't affected by how function is displayed; want output to be "True"*)
c (b – a)
(–a + b) c
True
Question
How do I define a function such that:
e1=func[c (b-a)]
e2=c (b-a) (
e1==e2
c (b – a)
(–a + b) c
True
Notes:
(1) I want to alter only the display format, not any other functionality. E.g., MMA should still recognize that e1 and e2 are mathematically equivalent, even though e1 was defined while the Orderless Attribute was absent from both Plus and Times.
(2) This is not quite a duplicate of Changing the display ordering of orderless functions? because there the OP was looking for a function that would dictate the display order, while I was looking for a simpler approach that merely displayed the output in the same order that I typed it. [So the function would just act to protect the display order, rather than specify it.]
(3)This was originally a question that ended up having two separate parts.
To make the information in this more readily searchable, I've divided this question into two (this being the first part). See: Dividing one thread into two
function-construction output-formatting sorting output pure-function
function-construction output-formatting sorting output pure-function
edited Jan 9 at 4:49
theorist
asked Dec 30 '18 at 1:55
theoristtheorist
1,179420
1,179420
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You could define a format that does this for you:
SetAttributes[orderlessForm, HoldFirst]
MakeBoxes[orderlessForm[expr_], form_] ^:= Internal`InheritedBlock[Times, Plus,
ClearAttributes[Times, Plus, Orderless];
MakeBoxes[expr, form]
]
Then:
orderlessForm[c (b - a)]
c (b - a)
And, the usual output when not using the wrapper:
c (b - a)
(-a + b) c
Note that the HoldFirst
attribute does most of the work.
Thanks Carl. One problem I notice is that orderlessForm alters some functionality. E.g.,e1 = orderlessForm[a + b];
e2 = orderlessForm[b + a];
e1 == e2
givesa + b == b + a
instead ofTrue
. I've added an edit at the end of my question to make this requirement explicit.
– theorist
Dec 30 '18 at 4:20
@theorist look at theFullForm
for what you see there and then see you can add anUpValue
toorderedForm
to make it disappear when used in any computation like this. But in general why do you need that requirement? Why can’t you just useFirst
first?
– b3m2a1
Dec 30 '18 at 15:35
@b3m2a1 Let me address each of your points in turn. (1): addUpValue
: Sorry, I don't understand where you wish to add that or what you mean by makingorderedForm
disappear. (2) Need for requirement: I thought I explained that at the end of my OP—I need expressions I define when usingorderlessForm
will behave normally. (3) UseFirst
first: Sorry, don't know what you mean here.
– theorist
Dec 30 '18 at 21:38
@theorist you define anUpValue
that handles working withPlus
or you useorderlessForm
only as a display form—e.g. useFirst
to strip it before doing any operations.
– b3m2a1
Dec 30 '18 at 21:45
@b3m2a1 Sorry, still don't understand; I'd need to see the actual code.
– theorist
Dec 30 '18 at 21:51
|
show 5 more comments
I decided to contact Wolfram Technical Support (WTS) on this one. Working together, we were able to find a construction that meets my needs:
SetAttributes[fun1, HoldAll]
fun1[expr_] := Module[, Print[Style[HoldForm[expr], 13]]; expr];
e1 = fun1[c*(b - a)];
e2 = c*(b - a)
e1 == e2
c (b-a)
(-a + b) c
True
The HoldAll statement is necessary because, without it, MMA would sort the function's argument into canonical order before HoldForm is applied:
fun2[expr_] := Module[, Print[Style[HoldForm[expr], 13]]; expr];
e1 = fun2[c*(b - a)];
(-a+b) c
A comment by Mr. Wizard (see below) indicates this can also be accomplished using a pure function:
fun3 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll];
e1 = fun3[c*(b - a)];
e2 = c*(b - a)
e1 == e2
c (b-a)
(-a + b) c
True
1
"Unfortunately, you can't apply HoldAll to a pure function" Actually you can, e.g.fun2 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll]
– Mr.Wizard♦
Jan 8 at 11:02
@Mr.Wizard Nice! Can this also be done using the#
&
syntax (which I normally associate with pure functions)?
– theorist
Jan 8 at 20:19
1
Yes, but the syntax is undocumented, and not really any cleaner than the form above. Its use is primarily in##
(SlotSequence
) which is not easy to emulate, and where scoping behavior of named parameters is a problem. See mathematica.stackexchange.com/q/29168/121 and specifically Leonid's answer for these advanced cases.
– Mr.Wizard♦
Jan 8 at 23:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188576%2foutputting-expressions-in-the-order-in-which-theyre-entered%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could define a format that does this for you:
SetAttributes[orderlessForm, HoldFirst]
MakeBoxes[orderlessForm[expr_], form_] ^:= Internal`InheritedBlock[Times, Plus,
ClearAttributes[Times, Plus, Orderless];
MakeBoxes[expr, form]
]
Then:
orderlessForm[c (b - a)]
c (b - a)
And, the usual output when not using the wrapper:
c (b - a)
(-a + b) c
Note that the HoldFirst
attribute does most of the work.
Thanks Carl. One problem I notice is that orderlessForm alters some functionality. E.g.,e1 = orderlessForm[a + b];
e2 = orderlessForm[b + a];
e1 == e2
givesa + b == b + a
instead ofTrue
. I've added an edit at the end of my question to make this requirement explicit.
– theorist
Dec 30 '18 at 4:20
@theorist look at theFullForm
for what you see there and then see you can add anUpValue
toorderedForm
to make it disappear when used in any computation like this. But in general why do you need that requirement? Why can’t you just useFirst
first?
– b3m2a1
Dec 30 '18 at 15:35
@b3m2a1 Let me address each of your points in turn. (1): addUpValue
: Sorry, I don't understand where you wish to add that or what you mean by makingorderedForm
disappear. (2) Need for requirement: I thought I explained that at the end of my OP—I need expressions I define when usingorderlessForm
will behave normally. (3) UseFirst
first: Sorry, don't know what you mean here.
– theorist
Dec 30 '18 at 21:38
@theorist you define anUpValue
that handles working withPlus
or you useorderlessForm
only as a display form—e.g. useFirst
to strip it before doing any operations.
– b3m2a1
Dec 30 '18 at 21:45
@b3m2a1 Sorry, still don't understand; I'd need to see the actual code.
– theorist
Dec 30 '18 at 21:51
|
show 5 more comments
You could define a format that does this for you:
SetAttributes[orderlessForm, HoldFirst]
MakeBoxes[orderlessForm[expr_], form_] ^:= Internal`InheritedBlock[Times, Plus,
ClearAttributes[Times, Plus, Orderless];
MakeBoxes[expr, form]
]
Then:
orderlessForm[c (b - a)]
c (b - a)
And, the usual output when not using the wrapper:
c (b - a)
(-a + b) c
Note that the HoldFirst
attribute does most of the work.
Thanks Carl. One problem I notice is that orderlessForm alters some functionality. E.g.,e1 = orderlessForm[a + b];
e2 = orderlessForm[b + a];
e1 == e2
givesa + b == b + a
instead ofTrue
. I've added an edit at the end of my question to make this requirement explicit.
– theorist
Dec 30 '18 at 4:20
@theorist look at theFullForm
for what you see there and then see you can add anUpValue
toorderedForm
to make it disappear when used in any computation like this. But in general why do you need that requirement? Why can’t you just useFirst
first?
– b3m2a1
Dec 30 '18 at 15:35
@b3m2a1 Let me address each of your points in turn. (1): addUpValue
: Sorry, I don't understand where you wish to add that or what you mean by makingorderedForm
disappear. (2) Need for requirement: I thought I explained that at the end of my OP—I need expressions I define when usingorderlessForm
will behave normally. (3) UseFirst
first: Sorry, don't know what you mean here.
– theorist
Dec 30 '18 at 21:38
@theorist you define anUpValue
that handles working withPlus
or you useorderlessForm
only as a display form—e.g. useFirst
to strip it before doing any operations.
– b3m2a1
Dec 30 '18 at 21:45
@b3m2a1 Sorry, still don't understand; I'd need to see the actual code.
– theorist
Dec 30 '18 at 21:51
|
show 5 more comments
You could define a format that does this for you:
SetAttributes[orderlessForm, HoldFirst]
MakeBoxes[orderlessForm[expr_], form_] ^:= Internal`InheritedBlock[Times, Plus,
ClearAttributes[Times, Plus, Orderless];
MakeBoxes[expr, form]
]
Then:
orderlessForm[c (b - a)]
c (b - a)
And, the usual output when not using the wrapper:
c (b - a)
(-a + b) c
Note that the HoldFirst
attribute does most of the work.
You could define a format that does this for you:
SetAttributes[orderlessForm, HoldFirst]
MakeBoxes[orderlessForm[expr_], form_] ^:= Internal`InheritedBlock[Times, Plus,
ClearAttributes[Times, Plus, Orderless];
MakeBoxes[expr, form]
]
Then:
orderlessForm[c (b - a)]
c (b - a)
And, the usual output when not using the wrapper:
c (b - a)
(-a + b) c
Note that the HoldFirst
attribute does most of the work.
answered Dec 30 '18 at 3:33
Carl WollCarl Woll
67.3k388175
67.3k388175
Thanks Carl. One problem I notice is that orderlessForm alters some functionality. E.g.,e1 = orderlessForm[a + b];
e2 = orderlessForm[b + a];
e1 == e2
givesa + b == b + a
instead ofTrue
. I've added an edit at the end of my question to make this requirement explicit.
– theorist
Dec 30 '18 at 4:20
@theorist look at theFullForm
for what you see there and then see you can add anUpValue
toorderedForm
to make it disappear when used in any computation like this. But in general why do you need that requirement? Why can’t you just useFirst
first?
– b3m2a1
Dec 30 '18 at 15:35
@b3m2a1 Let me address each of your points in turn. (1): addUpValue
: Sorry, I don't understand where you wish to add that or what you mean by makingorderedForm
disappear. (2) Need for requirement: I thought I explained that at the end of my OP—I need expressions I define when usingorderlessForm
will behave normally. (3) UseFirst
first: Sorry, don't know what you mean here.
– theorist
Dec 30 '18 at 21:38
@theorist you define anUpValue
that handles working withPlus
or you useorderlessForm
only as a display form—e.g. useFirst
to strip it before doing any operations.
– b3m2a1
Dec 30 '18 at 21:45
@b3m2a1 Sorry, still don't understand; I'd need to see the actual code.
– theorist
Dec 30 '18 at 21:51
|
show 5 more comments
Thanks Carl. One problem I notice is that orderlessForm alters some functionality. E.g.,e1 = orderlessForm[a + b];
e2 = orderlessForm[b + a];
e1 == e2
givesa + b == b + a
instead ofTrue
. I've added an edit at the end of my question to make this requirement explicit.
– theorist
Dec 30 '18 at 4:20
@theorist look at theFullForm
for what you see there and then see you can add anUpValue
toorderedForm
to make it disappear when used in any computation like this. But in general why do you need that requirement? Why can’t you just useFirst
first?
– b3m2a1
Dec 30 '18 at 15:35
@b3m2a1 Let me address each of your points in turn. (1): addUpValue
: Sorry, I don't understand where you wish to add that or what you mean by makingorderedForm
disappear. (2) Need for requirement: I thought I explained that at the end of my OP—I need expressions I define when usingorderlessForm
will behave normally. (3) UseFirst
first: Sorry, don't know what you mean here.
– theorist
Dec 30 '18 at 21:38
@theorist you define anUpValue
that handles working withPlus
or you useorderlessForm
only as a display form—e.g. useFirst
to strip it before doing any operations.
– b3m2a1
Dec 30 '18 at 21:45
@b3m2a1 Sorry, still don't understand; I'd need to see the actual code.
– theorist
Dec 30 '18 at 21:51
Thanks Carl. One problem I notice is that orderlessForm alters some functionality. E.g.,
e1 = orderlessForm[a + b];
e2 = orderlessForm[b + a];
e1 == e2
gives a + b == b + a
instead of True
. I've added an edit at the end of my question to make this requirement explicit.– theorist
Dec 30 '18 at 4:20
Thanks Carl. One problem I notice is that orderlessForm alters some functionality. E.g.,
e1 = orderlessForm[a + b];
e2 = orderlessForm[b + a];
e1 == e2
gives a + b == b + a
instead of True
. I've added an edit at the end of my question to make this requirement explicit.– theorist
Dec 30 '18 at 4:20
@theorist look at the
FullForm
for what you see there and then see you can add an UpValue
to orderedForm
to make it disappear when used in any computation like this. But in general why do you need that requirement? Why can’t you just use First
first?– b3m2a1
Dec 30 '18 at 15:35
@theorist look at the
FullForm
for what you see there and then see you can add an UpValue
to orderedForm
to make it disappear when used in any computation like this. But in general why do you need that requirement? Why can’t you just use First
first?– b3m2a1
Dec 30 '18 at 15:35
@b3m2a1 Let me address each of your points in turn. (1): add
UpValue
: Sorry, I don't understand where you wish to add that or what you mean by making orderedForm
disappear. (2) Need for requirement: I thought I explained that at the end of my OP—I need expressions I define when using orderlessForm
will behave normally. (3) Use First
first: Sorry, don't know what you mean here.– theorist
Dec 30 '18 at 21:38
@b3m2a1 Let me address each of your points in turn. (1): add
UpValue
: Sorry, I don't understand where you wish to add that or what you mean by making orderedForm
disappear. (2) Need for requirement: I thought I explained that at the end of my OP—I need expressions I define when using orderlessForm
will behave normally. (3) Use First
first: Sorry, don't know what you mean here.– theorist
Dec 30 '18 at 21:38
@theorist you define an
UpValue
that handles working with Plus
or you use orderlessForm
only as a display form—e.g. use First
to strip it before doing any operations.– b3m2a1
Dec 30 '18 at 21:45
@theorist you define an
UpValue
that handles working with Plus
or you use orderlessForm
only as a display form—e.g. use First
to strip it before doing any operations.– b3m2a1
Dec 30 '18 at 21:45
@b3m2a1 Sorry, still don't understand; I'd need to see the actual code.
– theorist
Dec 30 '18 at 21:51
@b3m2a1 Sorry, still don't understand; I'd need to see the actual code.
– theorist
Dec 30 '18 at 21:51
|
show 5 more comments
I decided to contact Wolfram Technical Support (WTS) on this one. Working together, we were able to find a construction that meets my needs:
SetAttributes[fun1, HoldAll]
fun1[expr_] := Module[, Print[Style[HoldForm[expr], 13]]; expr];
e1 = fun1[c*(b - a)];
e2 = c*(b - a)
e1 == e2
c (b-a)
(-a + b) c
True
The HoldAll statement is necessary because, without it, MMA would sort the function's argument into canonical order before HoldForm is applied:
fun2[expr_] := Module[, Print[Style[HoldForm[expr], 13]]; expr];
e1 = fun2[c*(b - a)];
(-a+b) c
A comment by Mr. Wizard (see below) indicates this can also be accomplished using a pure function:
fun3 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll];
e1 = fun3[c*(b - a)];
e2 = c*(b - a)
e1 == e2
c (b-a)
(-a + b) c
True
1
"Unfortunately, you can't apply HoldAll to a pure function" Actually you can, e.g.fun2 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll]
– Mr.Wizard♦
Jan 8 at 11:02
@Mr.Wizard Nice! Can this also be done using the#
&
syntax (which I normally associate with pure functions)?
– theorist
Jan 8 at 20:19
1
Yes, but the syntax is undocumented, and not really any cleaner than the form above. Its use is primarily in##
(SlotSequence
) which is not easy to emulate, and where scoping behavior of named parameters is a problem. See mathematica.stackexchange.com/q/29168/121 and specifically Leonid's answer for these advanced cases.
– Mr.Wizard♦
Jan 8 at 23:43
add a comment |
I decided to contact Wolfram Technical Support (WTS) on this one. Working together, we were able to find a construction that meets my needs:
SetAttributes[fun1, HoldAll]
fun1[expr_] := Module[, Print[Style[HoldForm[expr], 13]]; expr];
e1 = fun1[c*(b - a)];
e2 = c*(b - a)
e1 == e2
c (b-a)
(-a + b) c
True
The HoldAll statement is necessary because, without it, MMA would sort the function's argument into canonical order before HoldForm is applied:
fun2[expr_] := Module[, Print[Style[HoldForm[expr], 13]]; expr];
e1 = fun2[c*(b - a)];
(-a+b) c
A comment by Mr. Wizard (see below) indicates this can also be accomplished using a pure function:
fun3 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll];
e1 = fun3[c*(b - a)];
e2 = c*(b - a)
e1 == e2
c (b-a)
(-a + b) c
True
1
"Unfortunately, you can't apply HoldAll to a pure function" Actually you can, e.g.fun2 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll]
– Mr.Wizard♦
Jan 8 at 11:02
@Mr.Wizard Nice! Can this also be done using the#
&
syntax (which I normally associate with pure functions)?
– theorist
Jan 8 at 20:19
1
Yes, but the syntax is undocumented, and not really any cleaner than the form above. Its use is primarily in##
(SlotSequence
) which is not easy to emulate, and where scoping behavior of named parameters is a problem. See mathematica.stackexchange.com/q/29168/121 and specifically Leonid's answer for these advanced cases.
– Mr.Wizard♦
Jan 8 at 23:43
add a comment |
I decided to contact Wolfram Technical Support (WTS) on this one. Working together, we were able to find a construction that meets my needs:
SetAttributes[fun1, HoldAll]
fun1[expr_] := Module[, Print[Style[HoldForm[expr], 13]]; expr];
e1 = fun1[c*(b - a)];
e2 = c*(b - a)
e1 == e2
c (b-a)
(-a + b) c
True
The HoldAll statement is necessary because, without it, MMA would sort the function's argument into canonical order before HoldForm is applied:
fun2[expr_] := Module[, Print[Style[HoldForm[expr], 13]]; expr];
e1 = fun2[c*(b - a)];
(-a+b) c
A comment by Mr. Wizard (see below) indicates this can also be accomplished using a pure function:
fun3 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll];
e1 = fun3[c*(b - a)];
e2 = c*(b - a)
e1 == e2
c (b-a)
(-a + b) c
True
I decided to contact Wolfram Technical Support (WTS) on this one. Working together, we were able to find a construction that meets my needs:
SetAttributes[fun1, HoldAll]
fun1[expr_] := Module[, Print[Style[HoldForm[expr], 13]]; expr];
e1 = fun1[c*(b - a)];
e2 = c*(b - a)
e1 == e2
c (b-a)
(-a + b) c
True
The HoldAll statement is necessary because, without it, MMA would sort the function's argument into canonical order before HoldForm is applied:
fun2[expr_] := Module[, Print[Style[HoldForm[expr], 13]]; expr];
e1 = fun2[c*(b - a)];
(-a+b) c
A comment by Mr. Wizard (see below) indicates this can also be accomplished using a pure function:
fun3 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll];
e1 = fun3[c*(b - a)];
e2 = c*(b - a)
e1 == e2
c (b-a)
(-a + b) c
True
edited Jan 8 at 20:18
answered Jan 4 at 0:31
theoristtheorist
1,179420
1,179420
1
"Unfortunately, you can't apply HoldAll to a pure function" Actually you can, e.g.fun2 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll]
– Mr.Wizard♦
Jan 8 at 11:02
@Mr.Wizard Nice! Can this also be done using the#
&
syntax (which I normally associate with pure functions)?
– theorist
Jan 8 at 20:19
1
Yes, but the syntax is undocumented, and not really any cleaner than the form above. Its use is primarily in##
(SlotSequence
) which is not easy to emulate, and where scoping behavior of named parameters is a problem. See mathematica.stackexchange.com/q/29168/121 and specifically Leonid's answer for these advanced cases.
– Mr.Wizard♦
Jan 8 at 23:43
add a comment |
1
"Unfortunately, you can't apply HoldAll to a pure function" Actually you can, e.g.fun2 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll]
– Mr.Wizard♦
Jan 8 at 11:02
@Mr.Wizard Nice! Can this also be done using the#
&
syntax (which I normally associate with pure functions)?
– theorist
Jan 8 at 20:19
1
Yes, but the syntax is undocumented, and not really any cleaner than the form above. Its use is primarily in##
(SlotSequence
) which is not easy to emulate, and where scoping behavior of named parameters is a problem. See mathematica.stackexchange.com/q/29168/121 and specifically Leonid's answer for these advanced cases.
– Mr.Wizard♦
Jan 8 at 23:43
1
1
"Unfortunately, you can't apply HoldAll to a pure function" Actually you can, e.g.
fun2 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll]
– Mr.Wizard♦
Jan 8 at 11:02
"Unfortunately, you can't apply HoldAll to a pure function" Actually you can, e.g.
fun2 = Function[expr, Print[Style[HoldForm[expr], 13]]; expr, HoldAll]
– Mr.Wizard♦
Jan 8 at 11:02
@Mr.Wizard Nice! Can this also be done using the
#
&
syntax (which I normally associate with pure functions)?– theorist
Jan 8 at 20:19
@Mr.Wizard Nice! Can this also be done using the
#
&
syntax (which I normally associate with pure functions)?– theorist
Jan 8 at 20:19
1
1
Yes, but the syntax is undocumented, and not really any cleaner than the form above. Its use is primarily in
##
(SlotSequence
) which is not easy to emulate, and where scoping behavior of named parameters is a problem. See mathematica.stackexchange.com/q/29168/121 and specifically Leonid's answer for these advanced cases.– Mr.Wizard♦
Jan 8 at 23:43
Yes, but the syntax is undocumented, and not really any cleaner than the form above. Its use is primarily in
##
(SlotSequence
) which is not easy to emulate, and where scoping behavior of named parameters is a problem. See mathematica.stackexchange.com/q/29168/121 and specifically Leonid's answer for these advanced cases.– Mr.Wizard♦
Jan 8 at 23:43
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188576%2foutputting-expressions-in-the-order-in-which-theyre-entered%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown