leema basics

Here is an intro to some of the basics of Leema. The format will show some code, then explain the concepts used in that code. See what you think!

making a palindrome

This is a function that takes a string and returns it as a palindrome.

func palindrome(fwd: Str): Str ->
    let rev := str::reverse(fwd)
    "$fwd$rev"
--

Now, let’s break this down.

function declaration

All function declarations start with the func keyword.

palindrome_me is the name of this function. It takes one parameter, named fwd that is of type Str for string and also returns a Str.

The -> indicates the start of a block of code which is ended by the --. What happens in between is the body of the function. Think of the -- like an oldschool <hr/> tag in HTML.

Like many languages, Leema has a special main function to start your program. Whitespace is not significant in Leema so a function can also be written like this:

func main() -> print("this is main") --

function calls

Once a function is defined, you can call it. The palindrome above calls the reverse function from the str module and passes fwd as the one parameter. str::reverse(fwd)

let statements

Values are assigned labels using let statements. In this case, the result of str::reverse() is the value we want to assign and rev is the label we want to give it. Let statements can use pattern matching too, but we’ll show more of that later.

string interpolation

Variables can be turned into strings and mashed together with other strings using string interpolation. Within the string, just prefix the variable name with a $ and Leema will know to put the value of that variable in that part of the string.

In this case, fwd and rev are already strings, and they are mashed together into a single new string as the function result. Note that the last statement is returned as the function result, we don’t need to use an explicit return statement.

Previous – Setup Leema