Wolfram Language Tutorial

By Xah Lee. Date: . Last updated: .
XahTV 2021-06-02 Wolfram Language Basics for Industrial Programers

This is a simple and basic Wolfram Language tutorial so that you can start coding it in 1 hour. This tutorial is designed especially if you are familiar with a industrial language, for example: python, JavaScript, php, ruby, lisp.

See also: Get Wolfram Language Free

work in progress

Strings

String is quoted by double straight quote.

"this is a string"

To include newline, use \n. To include tab character, use \t.

"this is\na string"

you can have arbitrary unicode characters

"I ♥ cats"

String Functions

StringJoin["abc", "d"]
StringPart["abcde", 3]
StringLength["abc"]

To get a list of all string functions, type ?String* or the FullForm Information["?String*"]

Comment

(*this is comment.
can be multiple lines*)

Arithmetic

(* addition *)
3+2
Plus[3,4]

(* substraction *)
3-2
Plus[3,-2]

(* multiplication *)
3*2
Times[3,2]

(* division *)
3/2
Times[3, Power[2, -1]]

(* power. 3 raised to 2 *)
3^2
Power[3, 2]

(* remainder, or modular arithmetic *)
Mod[5, 2]

Exact/Symbolic Result vs Approximate/Numerical Result

All computation returns symbolic or exact result. For example, 3/2 returns just itself, not 1.5. And 6/4 returns the rational number 3/2. And 3/2 + 5/9 return 37/18

3/2
(* return a rational number *)

3/2 + 5/9
(* return a 37/18*)

If one of your expression contains approximate number, then the result may not be exact.

3/2 + 5/9.
(* return 2.05556 *)

To get numerical results, use N[expr].

N[3/2]
(*returns 1.5 *)

Variables

(* global variable, of current session  *)
x = 5

clear variable

Clear the value of x:

Clear[x]

local variable

To create local variables, use Module[{var1,var2}, expr] . Each of the var1 can have the form var1=val1

This is lexical scoped.

x = 5;
Module[{x}, x = 3; x^2]
x
(* x is still 5*)

True and False

True and False are builtin symbols to represent true and false.

3 > 2
(* return True *)

If then

If[3 > 2, "yes", "no"]

Everything in Wolfram Language is a expression. That means, everything returns a value, including the If function. There is no such thing as statements.

loop (procedural style)

Wolfram Language is a function language. However, it supports the traditional loops constructs such as for-loop, while-loop, do-loop. In general, you should not use them, because they are less efficient and ugly.

For-loop:

For[i = 1, i < 9, i++, Print[i]]

While-loop:

n = 1; While[n < 4, Print[n]; n++]

Here's Do-loop. It has many parameters controlling the iteration and nested iteration.

Do[expr,n]
evaluates expr n times.
Do[expr,{i,iMax}]
evaluates expr with the variable i successively taking on the values 1 through iMax (in steps of 1).
Do[expr,{i,iMin, iMax}]
starts with iMin.
Do[expr,{i,iMin, iMax, di}]
uses steps di.
Do[expr,{i,iMin, iMax, di}, {j,jMin, jMax, dj} ]
evaluates expr looping over different values of j etc. for each i.
Do[expr,{i,{i1, i2, i3 }}}]
uses the successive values i.

Example:

Do[Print[n^2], {n, 4}]
Do[Print[n,m], {n, 4}, {m, 2}]

Important: the forms

is called iteration specification. It is used in many of Wolfram Language functions.

loop (functional style)

In general, you should use the following functions for loop or iteration. These are functional style constructs.

generating lists

Range
Like python range but more powerful. Argument needs not be integer.
Table
For generating nested list (n-dimensional matrix). Like python's list comprehension but more powerful.

apply function to a list

Map
Like many language's map, but can map to specific level of a nested list.
MapIndexed
Like Map, but also feed the index info to the function.
MapAt
Map f at particular positions of a list.
MapThread
Like Map, but for function that takes more than 1 arg.

recursion of function with 1 arg

Nest
Recursion of a function n times.
NestList
Like Nest but return all steps.
NestWhile
Like Nest, but with a condition when to stop.
NestWhileList
Like NestWhile but return all steps.
FixedPoint
Recursion of a function until result no longer changes.
FixedPointList
Like FixedPoint but return all the steps.

recursion of function with 2 args

Fold
Similar to reduce in many programing languages.
FoldList
Like Fold but return all the steps.
FoldWhile
Like Fold but with a condition test to stop.
FoldWhileList
Like FoldWhile but return all the steps.

Note: there is a lot more.

Range[20, -20, -Pi]
Table[  Sin[x]/Pi^x, {x, 3, 10, 2}]

list

{3, 4, 5}

List can nest in any way.

{3, 4, {1,2}, 5}

list operations, part, slice, extract element



Association List

function



WolframLang Guide