FUNCTIONAL PROGRAMMING STYLE

Functional programming style applies functions to expression rather than through control of execution flow to achieve the computational result.

fac[n_]  :=  Times  @@  Range[n]

@@ is the Apply[ ] operator; Times @@ expr is short form for Apply[Times, expr].

Range[4]

Apply[Times,  {1, 2, 3, 4}]

Apply[Plus,  {1, 2, 3, 4}]

Observe that:

Times  @@  (a + b + c)

Map[f, expr] is another function of similar style.

Map[f,  h[a, b, c]]

Map[Sin,  {1,  Pi/2,  2  Pi}]

Concept of Pure Function in Mathematica

The first argument in Apply[ ], or Map[ ] requires just the name of the function, e.g., Sin, not Sin[x].  We call such objects pure functions.  You can think of a pure function as a mathematical operator, ready to act on an expression.   Here is a general way of defining a pure function.  Note that Function[x,x^2] is equivalent to #^2 &.

Function[x,  x^2]

Map[ #^2   &,  {1, 2, 3}]

Let me show you a program that find zero of a function, f(x)=0, by applying FixedPoint[ ]; another example of functional programming style.  First the built-in function:

?FixedPoint

newtonzero[f_,  x0_]  :=  
      Module[ {x,  fp = f'},  
x  =  
          FixedPoint[(# - f[#]/fp[#]) &,  
            x0]; 
x
]

newtonzero[(Cos[#] - #) &,  0.0]

FindRoot[ Cos[x] == x,  {x,  0}]

We can give newtonzero another definition so that it accept normal function (not pure function) as input.

newtonzero[expr_,  x_Symbol,  x0_Real]  := 
      newtonzero[Function[x, expr],  x0]

newtonzero[Cos[x]  -  x,  x,  0.3]

Here is the way usage message is entered for newtonzero, you get the message when enter ?newtonzero:

newtonzero::usage  = "newtonzero[f,x0] finds f[x]=0, with trial \
starting value x0.  f must be a pure function while x0 should be Real.  \
newtonzero[expr, x, x0] finds x such that expr=0, where expr is an expression \
in the variable x (a symbol), and x0 is the starting value (a Real). N.B. the \
iterations need not always converge."

More single-line Mathematica definitions:
Vector calculus

Grad[f_,  arg_]  :=  D[f, #] &  /@  arg

Div[v_,  arg_]  :=  Inner[D,  v,  arg,  Plus]

JocobianMatrix[f_,  v_]  :=  Outer[D,  f,  v]

TripleProduct[a_,  b_,  c_]  :=  Det[{a, b, c}]

Grad[x^2 + y^2 + z^2, {x, y, z}]