5
$\begingroup$

I would like to understand what happens when we include a comment at the end of a line that ends with a semicolon.

For example, if you execute

x;
%/.{x->1} (*some comment*);
%

you obtain 1. However, the following code:

x;
%/.{x->1};(*some comment*)
%

ignores the rule and the output is x.

The point is that if I remove the comment (*some comment*) from any of these two cases, the result is correct. So, what is the role of the position of the comment in all of this?

I do not know if it is a bug in my version (11.0) or if I have misunderstood something (related with the real meaning of ";" as a composition of expressions, or with the correct use of comments).

$\endgroup$
  • 1
    $\begingroup$ I just reworded the title to make your question more easily found and read. Roll back, if you do not like it. $\endgroup$ – gwr Jan 17 '19 at 9:59
6
$\begingroup$

The trouble comes from the number of input lines used: If I copy your second example it just takes two lines - not three.

Note the difference:

1;
2;
%

2

1;
2; %

1

You will get a 1 in both of your examples if you press Return before the last %. % really is Out[] and will reference the previous line of input.

Edit

One should note that Out references to the result of a CompoundExpression (cf. the documentation).

In[1]:= CompoundExpression[2^2,Null] (* 2^2; *)
In[2]:= %

Out[2]= 4

But the behavior of Out is different for other expressions:

In[3]:= Null
In[4]:= %

(* No output will be shown, e.g. Out[4]= Null *)

The In- and Out-numbers here are simply exemplary.

|improve this answer|||||
$\endgroup$
  • 3
    $\begingroup$ Yes, totally right! Indeed, taking the second example but without the semicolon, if you click in between the input and the comment and write ;, a little space appears after the semicolon. And it seems that the line break before the third line has been eliminated. But it is a bit confusing that the number of input lines you see do not coincide with the real one. $\endgroup$ – A. Jimenez Cano Jan 15 '19 at 18:28
  • 1
    $\begingroup$ @A.JimenezCano I think it is worth to write a bug report to the tech support on this. $\endgroup$ – Alexey Popkov Feb 11 '19 at 17:04

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.