PreviousUpNext

10.9.5  Curried Functions

Here is another case in which Mythryl functions appear to be taking more than one argument:

    linux$ cat my-script
    #!/usr/bin/mythryl

    fun join_strings_with_space  string_1  string_2
        =
        string_1 + " " + string_2;

    printf "join_strings_with_space \"abc\" \"def\" = \"%s\".\n" (join_strings_with_space "abc" "def");

    linux$ ./my-script
    join_strings_with_space "abc" "def" = "abc def".

Formally, we still have here functions which accept a single argument and return a single result. What is happening here formally is that we have two functions, the first of which accepts the string_1 argument and which then returns the second function, which accepts the string_2 argument and generates the final result.

The above code is in fact a shorthand for:

    linux$ cat my-script
    #!/usr/bin/mythryl

    fun join_strings_with_space  string_1
        =
        \\  string_2
            =
            string_1 + " " + string_2;

    printf "join_strings_with_space \"abc\" \"def\" = \"%s\".\n" (join_strings_with_space "abc" "def");

    linux$ ./my-script
    join_strings_with_space "abc" "def" = "abc def".

That this is more than a polite theoretical fiction is demonstrated by the fact that we can partially apply curried functions to actually obtain and use the intermediate anonymous functions:

    linux$ cat my-script
    #!/usr/bin/mythryl

    fun join_strings_with_space  string_1  string_2
        =
        string_1 + " " + string_2;

    prefix_with_abc = join_strings_with_space "abc";
    prefix_with_xyz = join_strings_with_space "xyz";

    printf "Prefixed with abc: \"%s\".\n" (prefix_with_abc "mno");
    printf "Prefixed with xyz: \"%s\".\n" (prefix_with_xyz "mno");

    linux$ ./my-script
    Prefixed with abc: "abc mno".
    Prefixed with xyz: "xyz mno".

For a more extended example of using partial application of curried functions, see the parsing combinators tutorial.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext