Everything Is an Expression
Prerequisites: Variables and assignment
The most important idea in the Wolfram Language: every piece of code
and data is an expression of the form head[arg1, arg2, ...].
FullForm
FullForm shows the internal form, without any special syntax:
FullForm[x + 2 y]
FullForm[{1, 2, 3}]
FullForm[a - b]
FullForm[x/y]
FullForm[a -> b]
Plus[x, Times[2, y]]
List[1, 2, 3]
Plus[a, Times[-1, b]]
Times[x, Power[y, -1]]
Rule[a, b]
x + 2 yis reallyPlus[x, Times[2, y]].- A list is
List[...]. - Subtraction is addition of
-1times something; division is multiplication by a power of-1.
Operators like +, *, ^, and { } are just convenient ways to write
these expressions.
Heads
Head returns the head of an expression — its "type":
Head[3]
Head[2.5]
Head[1/2]
Head["hi"]
Head[{1, 2}]
Head[x + y]
Head[f[x]]
Integer
Real
Rational
String
List
Plus
f
f[x] is a valid expression even though f means nothing yet — it just
stays f[x].
Parts
Because everything is an expression, the same functions work on lists, formulas, and more:
expr = x + 2 y + z^2;
Length[expr]
expr[[2]]
expr[[0]]
3
2 y
Plus
Part 0 is the head.
The four kinds of brackets
| Brackets | Meaning | Example |
|---|---|---|
( ) |
grouping | (a + b) c |
[ ] |
function arguments | Sin[x] |
{ } |
lists | {1, 2, 3} |
[[ ]] |
parts | list[[2]] |
Other ways to call a function
Sqrt[16]
Sqrt @ 16
16 // Sqrt
4
4
4
f @ x (prefix) and x // f (postfix) both mean f[x]. Postfix is handy
at the end of a line: expr // N, expr // Simplify.