PreviousUpNext

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

    #!/usr/bin/mythryl

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

    printf "%d\n" 3! ;

Running this yields:

    linux$ ./my-script
    6
    linux$

Judiciously used, this capability can significantly improve code readability.

Mythryl also supports a limited number of circumfix operators, including

    |x|
    <x>
    /x/
    {i}

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

    #!/usr/bin/mythryl

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

    a = -3;

    printf "%d\n" |a| ;

Running this yields:

    linux$ ./my-script
    3
    linux$

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

    <x|
    |x>

For example:

    #!/usr/bin/mythryl

    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!
    linux$

Comments and suggestions to: bugs@mythryl.org

PreviousUpNext