Block

Block[{x, y}, ....] is normally used to make the kernel to temporarily forget about any values that symbols (x,y) may have.  Contrary to what Module does the variables (x,y) inside Block are the same variables (x,y) outside the Block.  The only thing Block does is temporarily change or clear the values of these variables. Some applications are given in the grouped cells below.

The next cell clears any previous definitions that might interfere with the examples below.

Clear["Global`*"]

Temporarily clear the meaning of built in functions

If you need to write a program that you know may post messages you can suppress the display of messages during intermediate steps of your program.  As a specific example the expression in the next cell causes the kernel to evaluate Message[Prime::intpp, 2.25] inside the Block.  The head Message has no meaning while inside the Block, so the message is never printed.  Try evaluating Prime[2.25] without using  Block and a message will be printed (unless you turned off the message earlier).

Block[{Message}, Prime[2.25]]

Prime[2.25]

For another example of making the kernel forget what a built-in function does consider the expression below which is wrapped in HoldForm.

expr = HoldForm[{{Cos[π], 8^(1/2), Cos[π], (x y z)^2},  {12^(1/2), Sin[ArcCos[x]], 2, 0}}] ;

First we use ReleaseHold to see what the expression evaluates to without HoldForm.

ReleaseHold[expr]

{{-1, 2 2^(1/2), -1, x^2 y^2 z^2}, {2 3^(1/2), (1 - x^2)^(1/2), 2, 0}}

Suppose you want to evaluate this expression, but you don't want to evaluate 8^(1/2) ⟶22^(1/2) or 12^(1/2) ⟶23^(1/2). This can be done by using Block to make the kernel temporarily forget what Power does!  Recall n^(1/2)has the FullForm Power[n, 1/2].  Once inside Block we change the head of (expr) from HoldForm to List, and let it evaluate.  After evaluation is finished we change the head back to HoldForm so that 8^(1/2)and 12^(1/2) don't evaluate after we get out of the Block.  The curious thing about this trick is that we didn't have to Unprotect Power.  Another method of allowing only certain parts of an expression to evaluate is given in the discussion of Fold.

Block[{Power}, HoldForm @@ List @@ expr]

{{-1, 8^(1/2), -1, (x y z)^2}, {12^(1/2), (1 - x^2)^(1/2), 2, 0}}

Temporarily change the value of $DisplayFunction

The following line indicates the Default value Plot uses for the DisplayFunction option.

Options[Plot, DisplayFunction]

{DisplayFunction$DisplayFunction}

Besides making the kernel forget that a symbol has values you can use Block to temporarily use a new value for symbols. We can use Block to temporarily change the value of $DisplayFunction as in the cell below.  This creates the satisfying result of presenting only a single graphic, without typing  (DisplayFunction->Identity) four times!

Block[{$DisplayFunction = Identity},        p1 = Plot[Sin[t], {t, ... [GraphicsArray[{{p1, p2}, {p3, p4}}],      ImageSize-> {400, 260}   ] ;

Temporarily decrease $RecursionLimit  and/or  $IterationLimit

When you are writing preliminary drafts of a program you might want to use a lower value of $RecursionLimit and/or Iteration limit.  Using the code below you can make this change only take effect inside the Block.

ClearAll[f] ; f[n_] := 2 f[n - 1] ;     Block[{$RecursionLimit = 25, $IterationLimit = 100},     f[3]   ]

8388608 Hold[f[-19 - 1]]

{$RecursionLimit, $IterationLimit}

{256, 4096}

Temporarily clear the numeric value of a symbol

In the first cell below (demo) is an expression in (x).  In the second cell (x) has a numeric value is assigned and demo will turn out to be a constant if evaluated.  Since demo is a constant (provided x has a numeric value) the derivative of demo is zero.   Worse than that we see below that D[demo,x] doesn't even evaluate as we would like.

Clear[x] ; demo = x Exp[x] ;

x = 3 ;

D[demo, x]

∂_3 (3 ^3)

In the next cell we can evaluate the derivative of (demo) as if (x) had no numeric value.  This was possible because Block[{x}, ....] ensured that (x) had no values.  Apply[HoldForm,....] had to be used to prevent evaluating the result with numeric values for (x).

Dx = Block[{x}, Apply[HoldForm, {D[demo, x]}]]

^x + ^x x

In the next cell ReleaseHold is used to allow complete evaluation of the above derivative.

ReleaseHold[Dx]

4 ^3

Finally in the next cell Block[{x=a}, ....] causes (x) to temporarily evaluate to (a), and then evaluate the derivative of demo.

Clear[a] ; Block[{x = a}, D[demo, x]]

^a + a ^a

Modify a built-in function

Block can be used to modify the way built-in kernel functions evaluate in ways that are not otherwise possible.  For example suppose you wanted to change 'D' so that D[expr,x] would Expand expr before using the built-in definition of 'D'.

The definition for 'D' in the next cell is only used when the symbol $DoThis is True.  When this definition is used Block temporarily clears all values from the symbol $DoThis, and then evaluates D[Expand[expr],x].  If it were not for the use of Block{$DoThis},...] we would have infinite recursion since the new definition would call itself. However, once inside the Block $DoThis is not True, so the built-in definition of 'D' is used instead.

Unprotect[D] ; $DoThis = True ;  D[expr_, x_]/;$DoThis := Block[{$DoThis}, D[Expand[expr], x]   ]

D[(x + 5)^3, x]

∂_3512


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page