1
$\begingroup$

Focus on those 4 lines in the middle of the screenshot of the code. There are lines above and below those 4 lines which are not necessary for our discussion.

Issue: In the 1st of those 4 lines, I have removed the semicolon which was at the end of the line. When I did that Mathematica automatically introduces the 'multiply' symbol (visible in the screenshot). This is annoying. Any solutions?

Remark: When I copy only those 4 lines without the lines above and below them, and paste to a new notebook, the problem disappears.

Code screenshot

$\endgroup$
9
  • 3
    $\begingroup$ If you have f[a; b] then the semicolon is required. Removing it introduces a multiplication. If you have a; b at the top level, then the semicolon only serves to suppress the output. It can be replaced with a newline. In your screenshot it is not clear if the expressions are at the top level. This question can't really be answered without having access to the full cell expression (Ctrl-Shift-E) or the notebook. $\endgroup$
    – Szabolcs
    Sep 2, 2017 at 20:48
  • 2
    $\begingroup$ Take a look at Omitting ; can cause unexpected results in functions and Understand that semicolon (;) is not a delimiter in Common Pitfalls. $\endgroup$
    – Kuba
    Sep 3, 2017 at 5:57
  • 1
    $\begingroup$ Probably the more important question is: why exactly are the semicolons bothering you so much that you're picking on them? $\endgroup$ Sep 3, 2017 at 6:07
  • 1
    $\begingroup$ @Kuba I get your point but seems a little like saying there's no macroscopic kissing between people, in the end, because at the core it's all fundamental bosons and fermions. If some expression e.g. f,Map[x],List,Do, Greater is being used as the Head of an expression I suspect it's meaningful (and consistent with internal documentation) to call that Head a function. $\endgroup$ Sep 3, 2017 at 7:11
  • 1
    $\begingroup$ @Bingo So in your case f is Do. It's the same thing. Remember, everything is an expression. Also, you seem to be assuming that Mathematica inserts a multiplication sign: if only you could remove that, there would be no multiplication. This is not how it works. The visual $\times$ is a warning to you that the whitespace (newline) you just created will be interpreted as multiplication according to Mathematica syntax. Older versions don't show the $\times$ sign, but the newline is still interpreted as Times. $\endgroup$
    – Szabolcs
    Sep 3, 2017 at 8:46

1 Answer 1

5
$\begingroup$

You can force Mathematica into recognizing actual breaks (non times) with sufficient carriage returns and/or cell divisions.

That said I think you should reconsider why you want to get rid of your semicolon. If you merely want to output the results of what you're doing, but you're not going to do anything with that output other than look at it, I encourage Echo combined with semi-colon. It's much cleaner than dangling output and ensures no syntax glitches with unintended products.

x = 5 + y // Echo;
z = 17 + x // Echo;
x // Echo;

5+y

22+y

5+y


Update Szabolcs' comment points out something important worth emphasizing. If you're sitting inside of a function: e.g. Module, Block, With, looping functions like Do, Nest, FixedPoint, or even procedural conditional functions like If, Which, Switch, and you want to string a sequence of expressions for a single argument, the semi-colons (or equivalent ${}^1$) are mandatory. Only single expressions are allowed, which means you're either dealing with single expressions or technically CompoundExpressions. Any removal of a semi-colon automatically (unless it terminates the entire compound expression) means Product. A cell division will result in two cells that are meaningless etc, and no-amount of carriage returns will help.

${}^1$ By equivalent I simply mean the following. Only single expressions are allowed as arguments. We get around this naturally with the semi-colons, but what's happening is we actually getting around it by submitting a function with these separate expressions as separate arguments: f[a;b;c]= f[CompoundExpression[a,b,c]]. So by "the equivalent" I mean that you can always submit multiple expressions as part of an argument if you submit them as individual arguments to another function, like: List, CompoundExpression, etc.

$\endgroup$
4
  • $\begingroup$ Regarding @szabolcs comment: I was inside a Do loop actually, not a function. $\endgroup$ Sep 3, 2017 at 1:32
  • $\begingroup$ We mean function in the programming sense: Do[expr,list] is indeed a function -- the single brackets are the clue. In Mathematica anything that takes 0 or more arguments (0 like Break[], 2 like Do[expr,list]), is considered a function. Indeed on the Loops and Control Structures documentation we will find the following sentence: Functions like Do, Nest, and FixedPoint provide structured ways to make loops in Wolfram Language programs, while Throw and Catch provide opportunities for modifying this structure. $\endgroup$ Sep 3, 2017 at 4:32
  • 1
    $\begingroup$ Updated answer to clarify-- I'm sure you're not the only person who may have been confused by the terminology. $\endgroup$ Sep 3, 2017 at 4:48
  • $\begingroup$ 5 years of Mathematica experience, and I only just learnt to use "Echo" rather than omit a semi-colon. This explains a lot of errors... +1 $\endgroup$ Jan 6, 2020 at 3:11

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