PreviousUpNext

5.7.6  SML vs Mythryl Extended Example

At this point, a pair of matching code examples, one in SML, one in Mythryl, should give good intuition. First the SML version:

signature My_Api = sig
    sumtype Color = RED | GREEN | BLUE
    sumtype Point = TWO_D of (real * real) | THREE_D of (real * real * realt)
    val say_hello: unit -> unit
    val dist: ((real * real) * (real * real)) -> real
    val sum:  int list -> int
end

structure my_package :> My_Api = struct
    sumtype Color = RED | GREEN | BLUE
    sumtype Point = TWO_D of (real * real) | THREE_D of (real * real * real)
    fun say_hello () = print "Hello!\n"
    fun dist ((x0,y0), (x1,y1))
        =
        let val delta_x = x1 - x0
            val delta_y = y1 - y0
        in
            delta_x * delta_x
            +
            delta_y * delta_y
        end

    fun sum ints
        =
        let fun sum' ([],      result) => result
              | sum' (i :: is, result) => sum' (is, i + result)
        in
            sum' (ints, 0)
        end
end

Now the Mythryl version:

api My_Api {
    Color = RED | GREEN | BLUE;
    Point = TWO_D (Float, Float) | THREE_D (Float, Float, Float);
    say_hello: Void -> Void;
    mult: (Int, Int) -> Int;
    sum:  ((Float,Float), (Float,Float)) -> Float;
};

package my_package: My_Api {
    Color = RED | GREEN | BLUE;
    Point = TWO_D (Float, Float) | THREE_D (Float, Float, Float);
    fun say_hello () = print "Hello!\n";
    fun dist ((x0,y0), (x1,y1))
        =
        {   delta_x = x1 - x0;
            delta_y = y1 - y0;

            delta_x * delta_x
            +
            delta_y * delta_y;
        };
    fun sum ints
        =
        sum' (ints, 0)
        where
            fun sum' ([],     result) => result;
                sum' (i ! is, result) => sum' (is, i + result);
            end;
        end;
};


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext