0
$\begingroup$

I am trying to understand the exact behavior of the "enter" character in Mathematica. (I am not even sure what to call it: perhaps it is really the carriage return or newline character, which is input using the enter or return key. What I mean is whatever character gets input when I hit the key above "shift" on my mac.)

I haven't been able to find any Wolfram documentation about what how exactly the "enter" key works. I have noticed that it behaves very differently in at least four situations, described with examples below. What is the general behavior of "enter," and how does this general behavior explain the different specific behaviors I've encountered in the four cases below?


Case 1: Repeated evaluation

When I use the "enter" key to separate two lines of code in the same cell, both lines get evaluated and the results display in separate output cells:

Input:

1+1
2+2

Output (separate cells):

2

4

Case 2: Same as semicolon

If each line in the input code above is wrapped within InputForm, then for some reason only the output of the final line is evaluated. This behavior would also result from placing a semicolon after the first line.

Input:

InputForm[1 + 1]
InputForm[2 + 2]

Output:

4

Case 3: Same as space

Within Module, the enter character/key behaves the same as a space, which is often the same as multiplication:

Input:

Module[{},
 1 + 1
   2 + 2]

Output:

5

Case 4: Same as \n

When the "enter" key is used within a string in quotation marks, the result seems to be the same as entering the formatting character "\n":

Input:

Print["Line 1\nLine 2
 Line 3"]

Output:

Line 1
Line 2
Line 3
$\endgroup$

1 Answer 1

2
$\begingroup$
  • Case 1: Enter divides up independent lines of input. Nothing peculiar here, seems to me.

  • Case 2: that has to do with InputForm (which is an output wrapper and has its own magic behavior) and nothing to do with Enter there. If you try the following, you'll notice that the first output of InputForm is not shown either, but x is:

    InputForm[Sin[x]]
    x
    InputForm[2]
    
  • Case 3: Modules should contain a single expression; this can be an individual expression, or a "compound expression" separated by ;. In that context, any white space is interpreted as multiplication. I think you should start by understanding what ;, i.e. CompoundExpression, really does.

  • Case 4: If you evaluate the InputForm of your string, you will notice that your observation is literally true: Enter was replaced by "\n" in the string.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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