PreviousUpNext

1.2  Less coding effort.

Here is Quicksort in one line in Mythryl:

    fun qsort [] => [];  qsort (x!xs) => qsort (filter {. #a < x; } xs) @ [x] @ qsort (filter {. #a >= x; } xs);  end;

Admittedly, one would usually format it more like:

    fun qsort [] => [];
        qsort (x ! xs) => qsort (filter {. #a < x; } xs) @ [x] @ qsort (filter {. #a >= x; } xs);
    end;

Either way, this is a far cry from the hundred-plus lines of code of a typical C Quicksort implementation.

Here is a one-liner which finds and prints all C files under the current directory:

    #/usr/bin/mythryl
    foreach (dir_tree::files ".") {. if (#file =~ ./\\.[ch]$/)  printf "%s\n" #file;  fi; };

Here is another which generates a list of all Pythagorean triples (i,j,k) such that i2 + j2 = k2, all values being twenty or less:

    linux$ my

    eval:   [ (i,j,k) for i in 1..20 for j in i..20 for k in j..20 where i*i + j*j == k*k ];

    [ (3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (12, 16, 20) ]

Here is a Mythryl recursive descent parser for a fragment of English, written directly in vanilla Mythryl:

    verb      =  match [ "eats", "throws", "eat", "throw" ];
    noun      =  match [ "boy", "girl", "apple", "ball"   ];
    article   =  match [ "the", "a"                       ];
    adjective =  match [ "big", "little", "good", "bad"   ];
    adverb    =  match [ "quickly", "slowly"              ];

    qualified_noun =   noun   |   adjective  &  noun;
    qualified_verb =   verb   |   adverb     &  verb;

    noun_phrase    =             qualified_noun
                   | article  &  qualified_noun;

    sentence
        =
        ( noun_phrase  &  qualified_verb  &  noun_phrase     # "The little boy quickly throws the ball"
        |                 qualified_verb  &  noun_phrase     # "Eat the apple"
        | noun_phrase  &  qualified_verb                     # "The girl slowly eats"
        |                 qualified_verb                     # "Eat"
        );

This example owes its conciseness to deft use of:

(For the complete code and development of the above example see this tutorial, which also shows how to code it even more concisely.)

If you’ve only used Java and C++, phrases like partially applied curried function and parametric polymorphism probably sound like sheer gobbledygook, but once you have used them for a week or two you’ll wonder how you ever lived without them.

Collapsing pages of code into a few deft lines means less coding time, less debugging time, less maintenance time, and thus more time left to spend on the enjoyable aspects of software design and implementation.

Harder to quantify, but to many people even more important, is that deft, concise code is simply more satisfying to write. Verbose code is ugly; concise code is beautiful. Nobody enjoys creating ugliness; everyone enjoys creating beauty.

Mythryl unleashes your inner code poet.

Thanks to type safety and Hindley-Milner type inference, Mythryl combines the conciseness of scripting languages like Python with the efficiency of “strongly typed” compiled languages like C++.

This yields the best of both worlds from a coding effort point of view.

Every C programmer is drearily familiar with the fact that almost any significant maintenance change will result in multiple debugging runs, and more often than not some digging through coredumps in the debugger.

C programmers new to Mythryl are often startled to find that the typical maintenance change results in a program which runs as soon as it compiles.

At first, it feels like cheating.

This property is partly due to type safety and partly due to the somewhat mysterious fact that rich type systems tend to catch a lot more errors than by rights they should; most of the time a serious logic error turns out to trigger some sort of type error which results in it getting caught at compile time.

This does depend on the programmer working with the type system rather than against it. This means exposing as much as possible of the semantics of the program to the type system. One can, for example, treat all the keys to all the hashtables in a program as being of type String. But one can also assign different types to the keys for the different hashtables, in which case the typechecker will automatically flag as an error any attempt to use look up a key in the wrong hashtable.

The C programmer, laboring under a low-level type system incapable of expressing much of the required semantic subtlety, quickly acquires the habit of working around it via casts.

Learning to program effectively in Mythryl means unlearning this habit, learning instead to take creative advantage of the expressiveness of the Mythryl type system to describe to the compiler as much as possible of what is going on.

Be nice to your compiler, and it will save you untold hours of debug misery!


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext