5.3.14  Prefix, Postfix and Circumfix Operators

The Mythryl lexer distinguishes between certain infix and prefix arithmetic operators by the presence or absence of adjacent whitespace:

    a-b;               # binary infix
    a - b;             # binary infix
    a -b;              # unary prefix
    a- b;              # unary postfix

This is something of a kludge, but it allows us to use ascii - for both subtraction and negation and ascii * for both multiplication and dereferencing, making the most of the very limited number of available seven-bit ascii characters.

Thus, in Mythryl one can define factorial quite naturally as


    fun 0! =>  1;
        n! =>  n * (n - 1)! ;

    printf "%d\n" 3! ;

Running this yields:

    linux$ ./my-script

Judiciously used, this capability can significantly improve code readability.

Mythryl also supports a limited number of circumfix operators, including


This allows for example a more natural absolute value (or magnitude) function definitions:


    fun |x| =  (x < 0) ?? -x :: x;

    a = -3;

    printf "%d\n" |a| ;

Running this yields:

    linux$ ./my-script

Special for fans of quantum mechanics, Mythryl even allows you to define


For example:


    fun <x| =  printf "Wait a minute! You don't look like a quantum mechanic!\n";

    psi = 42;

    <psi| ;

The script output when run should be no surprise:

    linux$ ./my-script
    Wait a minute! You don't look like a quantum mechanic!

Comments and suggestions to: