1

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

Sashwat Tanay
  • 651
  • 4
  • 18
  • 3
    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. – Szabolcs Sep 02 '17 at 20:48
  • @Szabolcs : I am inside a Do loop actually, not inside a function f as suggested by you. – Sashwat Tanay Sep 03 '17 at 01:31
  • 2
  • 1
    Probably the more important question is: why exactly are the semicolons bothering you so much that you're picking on them? – J. M.'s missing motivation Sep 03 '17 at 06:07
  • @Kuba what do you mean there are no functions at the end? – John Joseph M. Carrasco Sep 03 '17 at 06:11
  • 1
    @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. – John Joseph M. Carrasco Sep 03 '17 at 07:11
  • 1
    @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. – Szabolcs Sep 03 '17 at 08:46
  • @Kuba I think both perspectives emphasize universality here in context. You want to emphasize universality of expressions which is great. There's also pedagogic value (esp for people new to mma) in realizing that every Head can parsed grammatically as a function, in the sense that functions expect single expression arguments. Poster's glitch in not recognizing Do as a function missed the universal point that in mma: functional arguments are individual expressions separated by commas. Multiple instructions are possible if wrapped by a head-- typically the function: CompoundExpression. – John Joseph M. Carrasco Sep 03 '17 at 08:55
  • @JohnJosephM.Carrasco I don;t agree with how you use 'function' name but this is subjective. Our discussion is off topic with respect to the OP so I'm deleting my comments, if you want you can ping me on chat to talk about that. – Kuba Sep 03 '17 at 09:21

1 Answers1

5

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.

  • Regarding @szabolcs comment: I was inside a Do loop actually, not a function. – Sashwat Tanay Sep 03 '17 at 01:32
  • 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. – John Joseph M. Carrasco Sep 03 '17 at 04:32
  • 1
    Updated answer to clarify-- I'm sure you're not the only person who may have been confused by the terminology. – John Joseph M. Carrasco Sep 03 '17 at 04:48
  • 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 – Colin MacLaurin Jan 06 '20 at 03:11