Mathematica Notation and syntax mods

后端 未结 3 629
迷失自我
迷失自我 2020-12-15 13:03

I am experimenting with syntax mods in Mathematica, using the Notation package.

I am not interested in mathematical notation for a specific field, but general purpos

3条回答
  •  庸人自扰
    2020-12-15 13:36

    Not a full answer, but just to show a trick I learned here (more related to symbol redefinition than to Notation, I reckon):

    Unprotect[Fold];
    Fold[f_, x_] :=
      Block[{$inMsg = True, result},
        result = Fold[f, First@x, Rest@x];
        result] /; ! TrueQ[$inMsg];
    Protect[Fold];
    
    Fold[f, {a, b, c, d}]
    (*
    --> f[f[f[a, b], c], d]
    *)
    

    Edit

    Thanks to @rcollyer for the following (see comments below).

    You can switch the definition on or off as you please by using the $inMsg variable:

    $inMsg = False;
    Fold[f, {a, b, c, d}]
    (*
    ->f[f[f[a,b],c],d]
    *)
    
    $inMsg = True;
    Fold[f, {a, b, c, d}]
    (*
    ->Fold::argrx: (Fold called with 2 arguments; 3 arguments are expected. 
    *)
    
    Fold[f, {a, b, c, d}]
    

    That's invaluable while testing

提交回复
热议问题