See also Mythryl tutorial, functions
Built-In Functions ∞
Mythryl comes with some functions built-in. These can be used as though you created them yourself. Here are some examples:
chomp()– Removes a trailing\nreverse()– Reverses a string. e.g. abc => cba-
strlen()– Counts the number of characters in a string.- This returns a number, so remember to use
%dforprintf.
- This returns a number, so remember to use
tolower()– Converts a string to lowercase-
toupper()– Converts a string to uppercase
(To my knowledge there isn’t a complete list of functions which can be used in this manner.)
Built-in functions can be used just like your own functions.
printf "%s\n" ( tolower( "EXAMPLE" ) ); # => # example
Functions with multiple parameters ∞
“Multiple parameters” isn’t precisely what’s going on, but it’s a useful enough description for now.
Let’s take substring() as our example. It’s used with three parameters:
substring( string, first_character, last_character );
For example:
printf "%s" ( substring( "some string", 0, 4 ) ); # => # some
Explicitly declaring types (Do this!) ∞
When I first started using Mythryl, I kept on using Ruby‘s “puts”, so I made a function:
fun puts( string_to_display ) = printf "%s\n" string_to_display; puts( "Hello, World!" ); # => # Hello, World!
This function expects to receive a string and it’s not supposed to
return anything.
fun puts( string_to_display: String ): Void = printf "%s\n" string_to_display; puts( "Hello, World!" ); # => # Hello, World!
Explicitly declaring types is optional, but I cannot stress enough how important it is to create all functions in this way.
We can take an earlier example, and explicitly declare types.
fun memory( _:Void ):String = "I can never remember this long phrase!\n"; printf "%s" ( memory() ); # => # I can never remember this long phrase!
Notice the first line, how the input is a Void, and it’s been signified by an underscore ( _ ).
Using other Mythryl functions ∞
There are many functions which are available to you, but which have to be specifically called upon.
a = "string"; b = string::length( a ); printf "%d" b; # => # 6
a = "string"; b = string::get( a, 1 ); printf "%c" b; # => # t
You can avoid prepending “foo::” by using include:
include string; a = "string"; b = get( a, 1 ); printf "%c" b; # => # t
include char; a = 'q'; b = next( a ); printf "%c" b; # => # r
Again, not everything needs to have an include statement to allow it to work, but if you get an unbound val error, that’s when you know you need to.
Finding other Mythryl functions ∞
There is much more to work with, but the above list will do fine for beginners.
These will be particularly useful:
- [http://mythryl.org/my-String.html |String]]
- [http://mythryl.org/my-Char.html |Char]]
-
[http://mythryl.org/my-Int.html |Int]]
Understanding the Mythryl documentation ∞
Let’s take an example from String
to_lower : String -> String;
- The function name is
to_lower - The function accepts a string – notice the colon (
:). -
The function returns a string – notice the
->
It’s basically:
name : input -> output;
From that one line, we can create a simple example:
printf "%s" ( string::to_lower( "EXAMPLE" ) );
This tutorial has not yet gone into the details of conditionals ( Bool ) or lists ( List(String ), List(Char ), etc.), so many of the functions you see will not make sense yet.
