Why did Mathematica choose brackets for function arguments over parentheses? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-08-18T03:46:21Z https://mathematica.stackexchange.com/feeds/question/72976 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://mathematica.stackexchange.com/q/72976 11 Why did Mathematica choose brackets for function arguments over parentheses? Nick https://mathematica.stackexchange.com/users/25022 2015-02-01T01:58:59Z 2018-03-26T03:01:54Z <p>This is a non-technical question. I'm just curious why <em>Mathematica</em> breaks the convention that parentheses are widely used for function arguments. What's the advantage of <code>f[x]</code> over <code>f(x)</code>? </p> <p>Again, for the derivative of a function, <code>f'(x)</code> and <code>f''(x)</code> are more familiar than <code>f'[x]</code> and <code>f''[x]</code>. I think these conventions in math textbooks have already existed for hundreds of years. </p> <p>If function arguments are denoted as <code>f(x)</code>, then <code>array[i]</code> could be used as array index. (c.f. <em>Mathematica</em> uses <code>array[[i]]</code> here.)</p> <p>To quote from the official documentation:</p> <blockquote> <p><strong>The Four Kinds of Bracketing in the Wolfram Language</strong></p> <p><code>(term)</code> parentheses for grouping </p> <p><code>f[x]</code> square brackets for functions</p> <p><code>{a, b, c}</code> curly braces for lists </p> <p><code>v[[i]]</code> double brackets for indexing (<code>Part[v, i]</code>)</p> </blockquote> <p>Are there any historical or antithetical reasons for choosing these notations?</p> https://mathematica.stackexchange.com/questions/72976/-/72977#72977 28 Answer by Chip Hurst for Why did Mathematica choose brackets for function arguments over parentheses? Chip Hurst https://mathematica.stackexchange.com/users/4346 2015-02-01T02:09:33Z 2015-02-01T02:20:58Z <p>The answer is quite simple. Most people want to multiply numbers without having to use the <code>*</code> symbol, e.g. <code>3x</code> vs <code>3*x</code>.</p> <p>So given that this exists in Mathematica, using <code>()</code> for function arguments would introduce ambiguity.</p> <p>Is <code>f(x + y)</code> meant to be <code>f[x + y]</code> or <code>f*(x + y)</code>?</p> <p>This is actually a problem Wolfram|Alpha can face since we try to allow for all forms of inputs.</p> <p>Other languages like C chose the other route, which means you <em>must</em> use <code>*</code> to indicate multiplication (something that annoys me). Given that Mathematica's original purpose was for mathematics, I think the right choice was made.</p> https://mathematica.stackexchange.com/questions/72976/-/118667#118667 12 Answer by J. M. is away for Why did Mathematica choose brackets for function arguments over parentheses? J. M. is away https://mathematica.stackexchange.com/users/50 2016-06-17T14:21:10Z 2016-06-17T14:21:10Z <p>Although Chip's answer already suffices to address the question, I would like to quote here a relevant part of the dialog by Theo Gray and Jerry Glynn in their book <em><a href="http://books.google.com/books?id=p0JzmwEACAAJ">Exploring Mathematics with Mathematica</a></em>; as there does not seem to be an easily accessible online version or preview of the book anywhere, I hope the quotation is useful:</p> <blockquote> <p>…</p> <p><strong>Theo:</strong> Satisfied? <em>Mathematica</em> also knows about a whole bunch of other functions, such as trigonometric functions. One of the weird things about <em>Mathematica</em> that tends to annoy people for a while is that you have to use square brackets and capital letters. For example:</p> <pre><code>Sin[1.2] 0.932039 </code></pre> <p><strong>Jerry:</strong> In other words, you're saying I can't type <code>sin 1.2</code> with no parentheses, or <code>sin(1.2)</code>, or <code>Sin(1.2)</code>, or <code>sin[1.2]</code>. I must type <code>Sin[1.2]</code>, exactly as you did. That seems like a real imposition.</p> <p><strong>Theo:</strong> Yes, you have to type <code>Sin[1.2]</code>, exactly. There are good reasons for both requirements, and we'll see why in later chapters. If you use one of the variations you suggested above, <em>Mathematica</em> will warn you that you are probably making a mistake. All of your variations are legal <em>Mathematica</em> input, but they don't mean what you want. (For example, <code>sin(1.2)</code> means the variable named <code>sin</code> multiplied by <code>1.2</code>.)</p> <p><strong>Jerry:</strong> OK, I'll live with the funny brackets for now.</p> <p>…</p> <p><strong>Jerry:</strong> …now, what about square brackets? Why can't I use <code>Sin(x)</code> instead of <code>Sin[x]</code>?</p> <p><strong>Theo:</strong> Good question! There is, in fact, a good reason. Ordinary mathematical notation is inconsistent here. Round parentheses are used to mean two completely different things in traditional notation: first, order of evaluation; second, function arguments. Consider the expression <code>k(b + c)</code>. Does this mean <code>k</code> times the quantity <code>b + c</code>, or does it mean the function <code>k</code> with the argument <code>b + c</code>? Unless you know from somewhere else that <code>k</code> is a function, or that <code>k</code> is a variable, you can't tell. It's a mistake to use the same symbols to mean these two completely different things, and <em>Mathematica</em> corrects this mistake by using round parentheses only for order of evaluation, and square brackets only for function arguments.</p> <p><strong>Jerry:</strong> That's a nice point. I never thought of that before. It shows how easily we adapt to nonsense. Aside from that, are you saying that mathematicians have been sloppy for centuries? That's a pretty strong statement!</p> <p><strong>Theo:</strong> Yes. Although I'm all in favor of interesting, quirky languages for writing novels and poetry (English comes to mind), it's really a bad idea to use an ambiguous language for something like mathematics. One of the great contributions of computer science to the world has been a powerful set of tools for thinking about what makes a language "good". </p> <p>An alternative would be to insist on using a <code>*</code> for all multiplication. Then <code>k(b + c)</code> would always mean the function <code>k</code>, and if you wanted it to mean multiplication you would have to use <code>k*(b + c)</code>. We decided it was better to remove an inconsistency than to force people to use an extra symbol. Another option would have been to have <em>Mathematica</em> "know" what was a variable and what was a function. This turns out to have serious consequences, and it's really not a good idea.</p> <p><strong>Jerry:</strong> Well, I didn't expect a lecture!</p> <p><strong>Theo:</strong> Sorry. Let's get back to the matter at hand. For functions, you use square brackets. Let's use the <code>Sin</code> function together with some round parentheses, to see how they fit:</p> <pre><code>Sin[1.2 (3 + 4)] (4 + 5) 7.69139 </code></pre> <p><strong>Jerry:</strong> This means, Find the sine of 1.2 times 7 and multiply that answer by 9.</p> <p><strong>Theo:</strong> Yes.</p> <p>…</p> </blockquote>