Applicative order Vs Normal order

showwaiyan

DamianZoirbile

Posted on July 21, 2021

Applicative order Vs Normal order

Yeah, the Topic is a little weird. You ever heard static language and dynamic language. So, this topic looks like the same static vs dynamic language.

Functional programming

In Functional programming, the evaluation of an expression rewrites the canonical form into the order(Evaluation Order).

What is rewriting the function??
It’s replacing the function body with the function call.
Eg Code Block:

//if the function the double the number
double x = x + x;
//if we call this function with a number
double 4
//It replaces by the body of the function "x + x" with argument
double 4 => 4 + 4
//so the answer is 8

Enter fullscreen mode Exit fullscreen mode
This is the evaluation of the function.

Evaluation Order

There's two important order rewriting.
1.Normal order
->It rewrites or execute leftmost occurrence of a function.
2.Applicative order
->It rewrites or execute innermost occurrence of a function.

Let do some codes

//the function double the number
double x = x + x;
//the function average the two number
average x y = (x + y) / 2;
Enter fullscreen mode Exit fullscreen mode

To avoid confusing the concept, let re-express operator function "+" , "*" and "/".

//so the function double change into 
double x = plus x x //change expression num1 + num2
//so the function average change into
average x y = divide (plus x y) 2 //change expression num1 / num2
Enter fullscreen mode Exit fullscreen mode

Let evaluate double(average 2 4)

Normal Order

Normal order execute leftmost part of function.

double (average 2 4) =>
plus (average 2 4) (average 2 4) =>
plus (divide (plus 2 4) 2) (average 2 4) =>
plus (divide 6 2) (average 2 4) =>
plus 3 (average 2 4) =>
plus 3 (divide (plus 2 4) 2) =>
plus 3 (divide 6 2) =>
plus 3 3 =>
6
Enter fullscreen mode Exit fullscreen mode

Applicative Order

Applicative Order execute innermost part of function.

 double (average 2 4) =>
 double (divide (plus 2 4) 2) =>
 double (divide 6 2) =>
 double 3 =>
 plus 3 3 =>
 6
Enter fullscreen mode Exit fullscreen mode

So, We see. Normal order evaluate many steps and it is a delay evaluation. It is lazy evaluation.
Applicative Order takes the few steps.

So, This is Applicative order languages vs normal order languages.

Here is the reference link

💖 💪 🙅 🚩
showwaiyan
DamianZoirbile

Posted on July 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related