6

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
user
  • 1,877
  • 10
  • 19
  • 7
    Why would you ever want to do that? I mean, +[1,2] is not even shorter than writing 1+2. – Sjoerd C. de Vries Nov 15 '15 at 22:27
  • Its more functional style. – Eric Brown Nov 15 '15 at 22:28
  • 2
    Plus[1,2] would be more functional style. – Sjoerd C. de Vries Nov 15 '15 at 22:29
  • 2
    It's more terse functional style. – Eric Brown Nov 15 '15 at 22:30
  • Here is a way to accomidate all unicode characters which I believe is kinda the intent of the question http://mathematica.stackexchange.com/questions/94349/how-can-we-accommodate-arbitrary-unicode-characters-in-symbol-names – William Nov 16 '15 at 00:56
  • 1
    Now, if you can get rid of the brackets and go full Polish... – J. M.'s missing motivation Nov 16 '15 at 03:06
  • @SjoerdC.deVries The point is that with one symbol you would be able to write expressions like +@@#&/@{{1,2},{3,4}} and yet benefit from Mathematica's algebraic capabilities, so that expressions like 1+1*2 would output 3 not 4. – user Nov 16 '15 at 09:55
  • 1
    1+1*2 already outputs 3. – shrx Nov 16 '15 at 10:04
  • 1
    Yes, but #/+@@# &@{1, 2, 3} doesn't output {1/6, 1/3, 1/2}. You can't use the same symbol for both infix and function head. – user Nov 16 '15 at 10:07
  • Well, that's what Normalize[] was meant for… – J. M.'s missing motivation Nov 16 '15 at 10:58
  • Such terseness is just not in the nature of Mathematica. If you're really looking for that kind of symbolic terseness, you might take a look at the free J programming language (http://jsoftware.com); in that language, +/ 2 3 4 gives result 9, and */2 3 4 the result 24 (and ^/ 2 3 4 the result 2.41785e24, where what ^/ does is to insert the power operator ^ between successive items of its argument). – murray Nov 16 '15 at 15:04

3 Answers3

13

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."

Eric Brown
  • 4,406
  • 1
  • 18
  • 36
  • 1
    Quite useful edit. I also propose to write x___ to handle zero number of arguments (sometimes it is useful). – ybeltukov Nov 15 '15 at 22:49
  • 1
    @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. – Szabolcs Nov 16 '15 at 11:09
  • @Szabolcs That's what it is! Thanks. I was confused because I had Notation working for * then not. – Eric Brown Nov 16 '15 at 12:52
9

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

+Sequence[1, 2, 3]
(*  6  *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
6

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 *)
Eric Brown
  • 4,406
  • 1
  • 18
  • 36
ybeltukov
  • 43,673
  • 5
  • 108
  • 212
  • 2
    You know, Plus is just one more keystroke and escape is far from home row. I'm rather with Sjoerd on this. – Michael E2 Nov 15 '15 at 23:06
  • Or how about p=Plus; t=Times; Fewer strokes, easier to remember. – bill s Nov 16 '15 at 02:45
  • @bills It was my first thought, but I decided that it is not very intuitive. – ybeltukov Nov 16 '15 at 03:12
  • 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? – user Nov 16 '15 at 09:33
  • 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. – user Nov 16 '15 at 09:45