6
$\begingroup$

Can one do something similar to +[1, 2]? The point is that with one symbol you would be able to write expressions like #/+@@# &@{1, 2, 3} and yet benefit from Mathematica's algebraic capabilities, so that expressions like 1+1*2 would output 3 not 4.

$\endgroup$
  • 7
    $\begingroup$ Why would you ever want to do that? I mean, +[1,2] is not even shorter than writing 1+2. $\endgroup$ – Sjoerd C. de Vries Nov 15 '15 at 22:27
  • 2
    $\begingroup$ Plus[1,2] would be more functional style. $\endgroup$ – Sjoerd C. de Vries Nov 15 '15 at 22:29
  • 2
    $\begingroup$ It's more terse functional style. $\endgroup$ – Eric Brown Nov 15 '15 at 22:30
  • 1
    $\begingroup$ Now, if you can get rid of the brackets and go full Polish... $\endgroup$ – J. M. will be back soon Nov 16 '15 at 3:06
  • 1
    $\begingroup$ 1+1*2 already outputs 3. $\endgroup$ – shrx Nov 16 '15 at 10:04
13
$\begingroup$

You can use the Notation package. It requires a GUI palette though.

Needs["Notation`"]

Once you have this package loaded, you can use the template to define:

Notation[+[x___] ==> Plus[x___]]

and then

+[1,2,3]
(* 6 *)

Similarly,

Notation[*[x___] ==> Times[x___]]

and so

*[2,3,4]
(* 24 *)

Note: A * typed as the first character of a cell converts it to an "Item Cell."

$\endgroup$
  • 1
    $\begingroup$ Quite useful edit. I also propose to write x___ to handle zero number of arguments (sometimes it is useful). $\endgroup$ – ybeltukov Nov 15 '15 at 22:49
  • 1
    $\begingroup$ @EricBrown Since Mathematica 10, typing * as the first character on the line converts it to an item cell. It's similar to how typing = as the first character is also special. $\endgroup$ – Szabolcs Nov 16 '15 at 11:09
  • $\begingroup$ @Szabolcs That's what it is! Thanks. I was confused because I had Notation working for * then not. $\endgroup$ – Eric Brown Nov 16 '15 at 12:52
9
$\begingroup$

For Plus, there's this, from How would I add together any list of arguments as a pure function?:

+Sequence[1, 2, 3]
(*  6  *)
$\endgroup$
6
$\begingroup$

You can use any character without built-in meaning like or (they differ from standard + and *). However, it is difficult to type them. So I propose to use meaningful Greek letters Σ and Π with shortcuts EscSEsc and EscPEsc respectively

Σ = Plus;
Π = Times;
Σ[1, 2]
Π[1, 2]
(* 3 *)
(* 2 *)

You can use this notation in other syntax constructions

Π @@ {1, 2, 3, 4}
(* 24 *)
$\endgroup$
  • 2
    $\begingroup$ You know, Plus is just one more keystroke and escape is far from home row. I'm rather with Sjoerd on this. $\endgroup$ – Michael E2 Nov 15 '15 at 23:06
  • $\begingroup$ Or how about p=Plus; t=Times; Fewer strokes, easier to remember. $\endgroup$ – bill s Nov 16 '15 at 2:45
  • $\begingroup$ @bills It was my first thought, but I decided that it is not very intuitive. $\endgroup$ – ybeltukov Nov 16 '15 at 3:12
  • $\begingroup$ It would be most useful if what ever symbol is used to denote summation would be used for both infix notation, like 1+1, and for function Head, like +[1,1]. Could one use one of these symbols in place of the current + symbol everywhere? $\endgroup$ – user Nov 16 '15 at 9:33
  • $\begingroup$ Sure you can write 1~Plus~1~Times~2, but the evaluation order isn't right: the output of the previous expression is 4 not 3. $\endgroup$ – user Nov 16 '15 at 9:45

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.