1
$\begingroup$

I was reading this documentation page : http://reference.wolfram.com/language/tutorial/DefiningOutputFormats.html

I really don't understand the point of the Format function.

Indeed, if I take the second example :

Format[xrep[n_]] := StringJoin[Table["x", {n}]]

Calling xrep would give the exact same result as defining things like :

xrep[n_]:=StringJoin[Table["x", {n}]]

So what does the function Format "add of new" here.

Same for the first example, I could write :

bin[x_, y_] := MatrixForm[{{x}, {y}}]

Instead of :

Format[bin[x_, y_]] := MatrixForm[{{x}, {y}}]

And everything would happen the same.

So I don't get what the Format function allows us to do more ?

$\endgroup$
2
$\begingroup$

Format allows you to show expression in a nice form while you are still able to work with them as before. By evaluating to _String/_MatrixForm you lose information/flexibility.

Consider:

bin /: bin[x_, y_]^n_. := bin[x, y^n];
Format[bin[x_, y_]] := Row[{"bin[", Panel[Column@{x, y}], "]"}];

bin[2, 2]^3
%^3

enter image description here

while

bin2 /: bin2[x_, y_]^n_. := bin2[x, y^n];
bin2[x_, y_] := Row[{"bin2[", Panel[Column@{x, y}], "]"}];

bin2[2, 2]^3
% ^3

enter image description here

$\endgroup$
  • $\begingroup$ Just a fast question before I can understand your answer : what does the "bin /: " does before you define your bin function ? Or at least what is the name of the symbol ? $\endgroup$ – StarBucK Apr 26 '18 at 9:52
  • $\begingroup$ @StarBucK see TagSetDelayed but if you select it and hit F1 it will take you there. $\endgroup$ – Kuba Apr 26 '18 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.