Skip to content

Instantly share code, notes, and snippets.

@mdg
Created October 1, 2018 21:00
Show Gist options
  • Save mdg/4beca4b048d1f5abc75f0c3417fac9eb to your computer and use it in GitHub Desktop.
Save mdg/4beca4b048d1f5abc75f0c3417fac9eb to your computer and use it in GitHub Desktop.

the basics

Here is an intro to the basics in 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment