Next: Renaming functions Up: Construction Previous: Reformatting expressions

Localised definitions

Rules defined using Mathematica's Format hold only in the body of a Block. They do not hold for functions executed within the Block. This behaviour can be illustrated by the following example:


In[1]:= mainformat[expr_,FortranForm]:= FortranForm[expr];
In[2]:= badformat[expr_] :=
          Block[{ArcSin,ASin},
            Format[ArcSin[y_],FortranForm]:= ASin[y];
            mainformat[expr,FortranForm]
         ];
In[3]:= badformat[ArcSin[a+b]]
Out[3]= ArcSin(a + b)
The format rule for ArcSin does not hold within the body of mainformat and is never applied to the function. It is therefore difficult to implement a modular design using this approach which, at the very least, makes code maintenance difficult. A preferred approach is outlined in the next section.


bondaren@thsun1.jinr.ru