Quick reverence about Wolfram Language programming, recap...
We can explore a bit more about Wolfram Language syntax (grammar):
With a AI LLM prompt, we get a beginners view of Wolfram Language Syntax:
In[]:=
ResourceFunction["ImportMarkdownString"][LLMSynthesize["Explain Wolfram Language sintaxe such as the symbols \" [ ] ( ) and functions, and general programming language usage"]]
Out[]=
The Wolfram Language, associated with Mathematica and Wolfram Alpha, is a symbolic programming language that emphasizes mathematical computation, data manipulation, and functional programming paradigms. Here's a brief explanation of its syntax and programming features: ​
Basic Syntax
​1. Function Application: ​
• Functions are expressed using square brackets
[]
. For example, to apply the function f to the argument x, you write
f[x]
.
​​
• Example:
Sin[Pi/4]
calculates the sine of 45 degrees.
​2. Lists: ​
• Lists are created using curly braces
{}
.
​​
• Example:
{1, 2, 3}
creates a list containing the numbers 1, 2, and 3.
​3. Groups: ​
• Parentheses
()
are used to group expressions and control the order of operations.
​​
• Example:
2*(3 + 4)
ensures that 3 and 4 are added before being multiplied by 2.
​4. Assignment: ​
• The assignment operator is
=
. To assign a value to a variable, you write
a = 5
.
​​
• Example:
x = f[y]
assigns the result of
f[y]
to x.
​​
Functions
​Functions in the Wolfram Language can take multiple arguments and can be defined in multiple ways:​1. Built-in Functions: ​
• Functions like
Sum
,
Integrate
,
Plot
, etc., are predefined.
​​
• Example:
Plot[Sin[x], {x, 0, 2 Pi}]
generates a plot of the sine function from 0 to
2π
.
​2. Defining Functions: ​
• You can create your own functions using
:=
(SetDelayed):
​
myFunction[x_]:=x^2+2*x+1
Insert and evaluate
Insert
Copy
​​
• In this case,
myFunction[2]
would return
7
.
​3. Anonymous Functions: ​
• Use
Function
or the shorthand
#
notation.
​​
• Example:
Function[x, x^2]
or
#^2 &
is a function that squares its argument.
​​
Control Structures
​1. Conditional Statements: ​
•
If
statements allow for conditional execution.
​
If[x>10,"Greater","Not Greater"]
Insert and evaluate
Insert
Copy
​2. Loops: ​
• You can use
For
,
While
, and
Do
for iterations.
​
For[i=1,i<=5,i++,Print[i]]
Insert and evaluate
Insert
Copy
​3. Pattern Matching: ​
• Functions can use patterns to match inputs. The underscore
_
represents a single argument, while
__
(double underscore) represents one or more arguments.
​
f[x_]:=x^2
Insert and evaluate
Insert
Copy
​​
General Programming Language Usage
​1. Module: ​
• You can create local variables with
Module
.
​
Module[{x,y},x^2+y^2]
Insert and evaluate
Insert
Copy
​2. Packages: ​
• You can organize code into packages and use
Needs
to include them.
​3. Mathematical Computation: ​
• The Wolfram Language is particularly strong in mathematical functions, symbolic computation, and data science operations.
​​
Examples
​​
• Defining Variables and Functions:
​​
a=5;​​b=10;​​sum[x_,y_]:=x+y;​​result=sum[a,b];(*resultis15*)
Insert and evaluate
Insert
Copy
​​
• Using Lists and Operations:
​​
list={1,2,3,4};​​squaredList=list^2;(*resultsin{1,4,9,16}*)
Insert and evaluate
Insert
Copy
​​
• Creating a Simple Plot:
​​
Plot[Sin[x],{x,0,2Pi}]
Insert and evaluate
Insert
Copy
​Overall, the Wolfram Language combines symbolic computation capabilities with a flexible syntax that supports both functional and procedural programming styles. Its rich set of built-in functions and symbolic capabilities make it particularly powerful for scientific computing, data analysis, and visualization.
We have some symbols for short input form, Map for example can be made as /@
In[]:=
Map[f,Range[-10,10]]
Out[]=
{f[-10],f[-9],f[-8],f[-7],f[-6],f[-5],f[-4],f[-3],f[-2],f[-1],f[0],f[1],f[2],f[3],f[4],f[5],f[6],f[7],f[8],f[9],f[10]}
It does the same as above:
In[]:=
f/@Range[-10,10]
Out[]=
{f[-10],f[-9],f[-8],f[-7],f[-6],f[-5],f[-4],f[-3],f[-2],f[-1],f[0],f[1],f[2],f[3],f[4],f[5],f[6],f[7],f[8],f[9],f[10]}
And we can create an inline function (pure function or anonymous function) using the symbols # (slot) and &:
In[]:=
Map[1/#&,Range[10]]
Out[]=
1,
1
2
,
1
3
,
1
4
,
1
5
,
1
6
,
1
7
,
1
8
,
1
9
,
1
10

Now the same result with short Map and pure function together:
In[]:=
1/#&/@Range[10]
Out[]=
1,
1
2
,
1
3
,
1
4
,
1
5
,
1
6
,
1
7
,
1
8
,
1
9
,
1
10

Another example, Map short is /@, and pure function with # and &, functional programming style:
In[]:=
If[#<=0,#^2,1/#]&/@Range[-10,10]
Out[]=
100,81,64,49,36,25,16,9,4,1,0,1,
1
2
,
1
3
,
1
4
,
1
5
,
1
6
,
1
7
,
1
8
,
1
9
,
1
10

Postfix notation with pure function:
Another example of functional programming, // is a post fix symbol:
It does the same as the code in the cell above:
A simple example about postfix symbol:
Nest list to recursively map the function f, functional programming style:
Using pure function (#, &) with NestList:
To learn more:
◼
  • Wolfram Documentation:
    https://reference.wolfram.com/language/
  • ◼
  • Wolfram Language: Introducción rápida para programadores
    https://www.wolfram.com/language/fast-introduction-for-programmers/es/?source=footer
  • ◼
  • Mathematica y Wolfram Language Introducción rápida para estudiantes de matemáticas
    https://www.wolfram.com/language/fast-introduction-for-math-students/es/?source=footer
  • ◼
  • How to | Use Brackets and Braces Correctly
    https://reference.wolfram.com/language/howto/UseBracketsAndBracesCorrectly.html
  • ◼
  • Wolfram Language Syntax
    https://reference.wolfram.com/language/guide/Syntax.html