WolframLang Syntax/Operators

By Xah Lee. Date: . Last updated: .

Intro

WolframLang has many operators and syntactic shortcuts. (or, magic characters)

In general, WolframLang syntax has the form:

• a
• a[]
• a[b]
• a[b,c]
• a[b,c ...]

where any of the a, b, c again has the form f[...], nesting arbitrarily, or they are atoms. (atom is a variable name, function name, string, number.)

The pure nesting of f[...] is called FullForm. You can type FullForm[Hold[expr]] to find out the fully nested form of any WolframLang code.

Too much nesting is hard to read.

WolframLang has syntax shortcuts for many commonly used functions. For example, you can write Plus[3,4] as 3+4, and List[3,4] as {3,4}, and Equal[x,y] as x == y, and Set[x,3] as x = 3, etc.

Example of FullForm syntax:

CompoundExpression[ Set[cArray, CoordinateBoundsArray[List[List[-1, 1], List[-1, 1]], Into]], Set[hLines, Map[Function[BlockMap[Line, Slot, 2, 1]], cArray]]]

Here's the above code using short syntax, called InputForm:

cArray = CoordinateBoundsArray[{{-1, 1}, {-1, 1}}, Into];
hLines = (BlockMap[Line, #1, 2, 1] & ) /@ cArray

This page gives a practically complete list of such syntactic shortcuts in WolframLang.

general/common syntax

Aaa
builtin symbols begin with capital letter. e.g. Sin[Pi]
\$Aaa
builtin variables begin with \$. e.g. \$System, \$Version, \$ProcessorCount
[]
square brackets are for function arguments. e.g. Sin[Pi]
()
parenthesis are for grouping precedence. e.g. (3+2)*2
{}
same as List[]
"abc"
double quote is for string delimiter. e.g. "abc"
\[Name]
represent a special character, such as \[Pi] (π), \[Element] (∈), etc. [see How Wolfram Language does Unicode?]
a`b
grave accent mark is symbol context separator. b in context a.
x = 3;
Context[x]
(* Global` *)

Global`x
(* 3 *)
(* blab *)
Comment.
expr1;expr2
same as CompoundExpression[expr1,expr2] CompoundExpression
?a
same as Information["a"] Information
%n
same as Out[n] Out
%
same as Out[-1]
%%
same as Out[-2]. And so on for %%%, etc.

Pure Function

body&
same as Function[body] Function
x|->body
same as Function[{x},body]
#
same as Slot Slot
#n
if n is a integer, then same as Slot[n].
if n is a name (letter sequence), then same as Slot["name"].
##
same as SlotSequence[] SlotSequence
##n
same as SlotSequence[n]

Function Application

f@x
prefix form. same as f[x]
x//f
postfix form. same as f[x]
a ~f~ b
infix form. same as f[a,b]
e[[i]]
same as Part[e,i] Part
f/@x
same as Map[f,x] Map
f@@x
same as Apply[f,x] Apply
f@@@x
same as Apply[f,x,{1}].

Function composition

f@*g
same as Composition[f,g] Composition
f/*g
same as RightComposition[f,g] RightComposition

Assignment

a=b
same as Set[a,b] Set
a:=b
same as SetDelayed[a,b] SetDelayed
f//=x
same as x = f[x].

Logic

a==b
same as Equal[a,b] Equal
a!=b
same as Unequal[a,b] Unequal
a===b
same as SameQ[a,b] SameQ
a=!=b
same as UnsameQ[a,b] UnsameQ

string

a<>b
same as StringJoin[a,b] StringJoin
a~~b
same as StringExpression[a,b] StringExpression

<<file
same as Get[file] Get
>>file
same as Put[file] Put
>>>file
same as PutAppend[file] PutAppend

Rules

a->b
same as Rule[a,b] Rule
a:>b
same as RuleDelayed[a,b] RuleDelayed
a/.b
same as Replace[a,b] Replace

pattern matching

_
same as Blank Blank
__
same as BlankSequence[] BlankSequence
___
same as BlankNullSequence[] BlankNullSequence
a_
same as Pattern[a, Blank[]]
a__
same as Pattern[a, BlankSequence[]]
a___
same as Pattern[a, BlankNullSequence[]]
a:b
if a is a symbol, then it's same as Pattern[a,b].
if a is a pattern, then it's same as Optional[a,b]. Optional
p..
same as Repeated[p] Repeated
a|b
same as Alternatives[a,b] Alternatives
p/;t
same as Condition[p,t] Condition
p?t
same as PatternTest[p,t] PatternTest

number literal

digits.digits
approximate number (aka float). e.g. 16.8
base^^digits
integer in specified base. Base up to 36, using English letters after 9. Letter case does not matter.
2^^10 === 2
2^^11 === 3
16^^a === 10
16^^f === 15
16^^ff === 255
36^^a === 10
36^^z === 35
36^^z1 === 1261
base^^digits.digits
approximate number in specified base.
significand*^n
scientific notation ( significand * 10^n )
1.2*^2 == 120.
3.2*^-2 == 0.032
base^^significand*^n
scientific notation in specified base ( significand * base^n )
number`
machine‐precision approximate number. e.g. 1.23`
number`s
arbitrary‐precision number with precision s.
3.1`20 == 3.1
number``s
arbitrary‐precision number with accuracy s
3.1``20 == 3.1

arithmetics

a+b
same as Plus[a,b]
-b
same as Times[-1,b]
a*b
same as Times[a,b]
a b
same as Times[a,b] if a and b are numbers or symbols.
a/b
same as Times[a, Power[b, -1]]. If a,b are integers, then same as Rational[a,b] Rational
a^b
same as Power[a,b]

a&&b
same as And[a,b]
a||b
same as Or[a,b]
!a
same as Not[a]

misc

symbol::tag
same as MessageName[symbol,tag].
Typically used to give a doc string of a function. example:
MessageName
xdom
same as Element[x,dom] Element
x \[Distributed] dist
same as Distributed[x,dist] Distributed
a \[UndirectedEdge] b
same as UndirectedEdge[a,b] UndirectedEdge
a \[DirectedEdge] b
same as DirectedEdge[a,b] DirectedEdge

Sample Code

here's sample WolframLang code in FullForm syntax:

CompoundExpression[
Set[geoInv,
Function[With[List[Set[x, Dot[Slot, Slot]]],
If[Less[x, 1.`*^-8], Slot, Times[Slot, Power[x, -1]]]]]],
Set[gridDivN, 16], Set[nGon, 4],
Set[rr, Times[Times[Times[2, Power[gridDivN, -1]], Power[2, -1]],
Sqrt, 0.7`]], Set[\[Alpha], Times[2, Times[Pi, Power[8, -1]]]],
Set[gridPoints,
Map[geoInv,
CoordinateBoundsArray[List[List[-1, 1], List[-1, 1]],
Into[gridDivN]], List]],
Set[gp1, With[
List[Set[sq,
Map[Function[Times[List[Cos[Slot], Sin[Slot]], rr]],
Range[\[Alpha],
Plus[Times[2, Pi], \[Alpha],
Times[-1,
Times[2, Times[Times[Pi, Power[nGon, -1]], Power[2, -1]]]]],
Times[2, Times[Pi, Power[nGon, -1]]]]]]],
Map[Function[List[x],
GraphicsComplex[
Map[Function[Plus[x, Slot]], Times[sq, Dot[x, x]]],
Line[Append[Range[nGon], 1]]]], gridPoints, List]]],
Set[gp2, ReplaceAll[gp1,
RuleDelayed[
GraphicsComplex[Pattern[x, Blank[]], Pattern[r, BlankSequence[]]],
GraphicsComplex[Map[geoInv, x], r]]]],
Set[gr1, Graphics[gp1, Rule[Axes, True]]],
Set[gr2, Graphics[gp2, Rule[Axes, True]]],
GraphicsGrid[List[List[gr1, gr2]]]]

here's the same code using short syntax:

geoInv = ((With[{x = # . #}, If[x < 0.00000001, #, #/x]]) &);

gridDivN = 16;
nGon = 4;
rr = (2/gridDivN)/2 (Sqrt@2) .7;
\[Alpha] = 2 Pi/8;

gridPoints =
Map[geoInv,
CoordinateBoundsArray[{{-1, 1}, {-1, 1}}, Into@gridDivN], {2}];

gp1 = With[{sq = ((({Cos[#], Sin[#]} rr) &) /@
Range[\[Alpha], 2 \[Pi] + \[Alpha] - 2 \[Pi]/nGon/2,
2 \[Pi]/nGon])},
Map[Function[{x},
GraphicsComplex[((x + #) &) /@ (sq x . x),
Line@Append[Range@nGon, 1]]], gridPoints, {2}]];

gp2 = gp1 /.
GraphicsComplex[x_, r__] :> GraphicsComplex[geoInv /@ x, r];

gr1 = Graphics[gp1, Axes -> True];
gr2 = Graphics[gp2, Axes -> True];

GraphicsGrid[{{gr1, gr2}}]