Skip to content

Tutorial

Christopher Hittner edited this page Dec 20, 2017 · 4 revisions

If you are new to Lomda, you should read over this guide (or at the very least skim over it). In this tutorial, I will demonstrate how to use the various features of Lomda.

Getting Started

If you have not installed Lomda, see the readme for more information on how to install the interpreter. To use the interpreter, one can either provide a source file to run code from (ex: ./lomda), or none if one wishes to directly access the interpreter (ex: ./lomdaa source.lom). Source files should have the .lom extension.

Lomda has several flags available that can be specified from the command line. These are:

Flag Description
-v/--verbose Verbose mode. This is for development of the language, and will display various debug info.
--version Displays the version and exists.
--werror Interprets warnings as errors. For instance, a warning is thrown when a program's syntax tree is ambiguous.

Arithmetic

Performing simple mathematical operations can be done exactly as one would expect.

> 1 + 2
3
> 3 * 5
15
> 3 * 4 - 7
5

Variables

In order to use variables, one must declare it.

> let x = 2; x + 3
5

Notice the presence of a single semicolon. In Lomda, programs do not consist of semicolon-terminated statements, but are instead built from semicolon-separated statements.

In Lomda, we can define multiple variables on different lines or on the same line. Thus, the following are identical programs:

> let x = 2; let y = 3; x + y
> let x = 2, y = 3; x + y

Functions

In Lomda, all functions are lambdas with recursive functionality. We can define one as follows:

> lambda (x) x
λx.x | {}

The notation for function output follows that of the lambda calculus, but is not identical. For instance, Lomda permits the use of multivariable functions:

> lambda (x, y) x + y
λx,y.x + y | {}

Furthermore, we can store a lambda into a variable and do things with it:

> let f = lambda (x) x; f
λx.x | {f := λ}
> let inc = lambda (x) x+1; f(2)
3

Note that in the first of the two programs, the output is different from before, notably because of the different environment available to the lambda. This is because the variable declaration expressions set up the lambdas to use the scope resulting from the variable declarations. In other words, one can define mutually recursive functions.

> let f = lambda (x) x, g = lambda (x) x; f
λx.x | {g := λ, f := λ}

Conditionals

Lomda also allows for boolean conditionals and branching. One can perform comparisons with any of the C-style operators (==, !=, >, etc.), as well as some pythonic operators (is, equals, not, etc.). Some examples include:

> 6 >= 2
true
> true and not false
true
> true is not true
false
> 6 is 6
true

We can also perform branching operations:

> if 6 is 7 then 1 else 0
0
> if true is not false and 7 < 13 then 17 else 3
17
Clone this wiki locally