WolframLang Syntax/Operators

By Xah Lee. Date: . Last updated: .

Video Tutorial

Xah Talk Show 2021-12-01 WolframLang vs Lisp Syntax Comparison

Intro

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

In general, WolframLang syntax has the form:

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

The pure nesting of f[etc] is called FullForm. You can type FullForm[Hold[expr]] to see 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.

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

Example of FullForm vs shortcut syntax

Example of FullForm syntax:

CompoundExpression[

 Set[ cArray,
  CoordinateBoundsArray[
   List[List[-1, 1], List[-1, 1]],
   Into[4]
  ]
 ],

 Set[
  hLines,
  Map[
   Function[
    BlockMap[Line, Slot[1], 2, 1]
   ],
   cArray
  ]
 ]
]

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

cArray = CoordinateBoundsArray[{{-1, 1}, {-1, 1}}, Into[4]];
hLines = (BlockMap[Line, #1, 2, 1] & ) /@ cArray
WolframLang FullForm 2022-05-04
WolframLang InputForm and FullForm

general/common syntax

Aaa
builtin function or other Symbol names begin with capital letter. e.g. Sin[Pi]
$Aaa
builtin variable names 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
{args}
same as List[args]
"abc"
double quote is for string delimiter. e.g. "abc"
\[Name]
represent a special character, such as \[Pi] (π), \[Element] (∈), etc. [see Wolfram Language and Unicode]
a`b
grave accent mark is context separator for Symbol. (It's a namespace separator. similar to the slash in file path separator, or dot used by golang, python, java.)
x = 3;
Context[x]
(* Global` *)

Global`x
(* 3 *)
(* blab *)
Comment. Can be nested. Can be multi-line.
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.

sub-expression

e[[i]]
same as Part[e, i] Part

Pure Function

body&
same as Function[body] Function
x|->body
same as Function[{x}, body]
#
same as Slot[1]. Use to stand for first parameter of function. 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 notation. same as f[x]
x//f
postfix notation. same as f[x]
a ~f~ b
infix notation. same as f[a, b]
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

read/write file

<<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[]] Pattern
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]

logic

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:
addPi::usage = "addPi[x] adds Pi to x."
addPi=Function[{x},x+Pi];
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[1], Slot[1]]]],
    If[Less[x, 1.`*^-8], Slot[1], Times[Slot[1], Power[x, -1]]]]]],
 Set[gridDivN, 16], Set[nGon, 4],
 Set[rr, Times[Times[Times[2, Power[gridDivN, -1]], Power[2, -1]],
   Sqrt[2], 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[2]]],
 Set[gp1, With[
   List[Set[sq,
     Map[Function[Times[List[Cos[Slot[1]], Sin[Slot[1]]], 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[1]]], Times[sq, Dot[x, x]]],
      Line[Append[Range[nGon], 1]]]], gridPoints, List[2]]]],
 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}}]

WolframLang in Depth

Basics

Comment, Print

String

Arithmetic

List

Expression, Atom, Head

Boolean

Conditional

Variable

Loop

Data Structure

Function

Pattern Matching

Advanced

Shell Tasks

Misc