/AntLang Tutorial

/Author: Anthony Cipriano

About the tutorial

AntLang is a new programming language, which indeed is very under-documented. However, this tutorial should bring you through the steps from the installation to the first library.

Installation

First you have to install AntLang. If there is no executable for your OS, check out the build instructions.

After you have downloaded the file, you may have to run the following command to get execution permition.

chmod +x ./ant

Then you can run the executable.

./ant

AntLang can crash your machine, if you write bad code. Therefore you can "sandbox" it, by giving it limited access to your memory for safety.

./ant +RTS -M100m #i.e. 100MB

For a better experience you can use the external program rlwrap. It isn't needed to follow this tutorial, so don't worry if you haven't installed it.

rlwrap ./ant +RTS -M100m

This command should open a top-level. To test it type in the following.

echo["Hello!"]

If this prints the message, everything is setted up correctly.

On Windows, you can simply call the executable with the shown arguments.

ant.exe +RTS -M100m

Arithmetic

AntLang can operate on numeric data, like integers or doubles.

For example enter the following calculation (whitespace is important).

2 + 2

This should return 4.

You can write more complex expressions, too. However, the result may differ from what you expect it to be.

1 % 2 + 3 (AntLang uses `%'-sign for division)

If you evaluate this using classic precedence rules you get back 3.5, but AntLang says it is 0.2.

This is a very important lesson in learning to master AntLang: It evaluates from right to left.

This seems weird at first, but if you think about it, it is very simple.

You don't have to remember rules of precedence, because whatever is on the right is done first. No exception.

If you want to break this behavoir, you can use parenthesis.

(1 % 2) + 3

Now you should get back 3.5.

You can also do floating-point number arithmetic.

0.2 + 0.1

This returns a value very near to 0.3 (but not exactly 0.3).

However, many languages behave like this, so this should not be a reason to hate AntLang.

AntLang supports subtraction (-), multiplication (*) and exponentation (expt), too.

If you may find the name for exponentation weird, here is how you can use it like any other operator.

8 expt 2
64 expt 0.5

Comparision

You can compare values using AntLang.

The comparision operators are greater-than (gt), lower-than (lt), equal (eq), not-equal (ne), greater-or-equal (ge) and lower-or-equal (le)

4 le 2 + 2

The rule learned in the arithmetic section, applies to comparision, too.
Therefore "2 + 2 eq 4" means "2 + (2 eq 4)" and not "(2 + 2) eq 4". You have to put parenthesis there.

More functions

AntLang has nothing like operators. They are normal functions, like sin or echo.

You can apply a function to its arguments using M-Expressions.

sin[2]

Since operators are normal functions, they can be applied the same way.

+[2; 2] eq 2 + 2

If a function takes two arguments, you can put it between the arguments, for better readability.

write["file.txt"; "Hello, World!"]
"file.txt" write "Hello, World!"

The write function performs some file-I/O and is just an example. This works for every other function, too.

Variables

You know how to perform several computations in AntLang, but sometimes you want to store a value to a variable, to use it later

If you already know a programming language like Rust or Nim, be careful: AntLang variables are immutable.

Once they have a value it can't (or at least shouldn't) change.

Therefore a variable is only another name for its value.

pi: 3.141592654

You can assign a variable to a value like shown above.

More formal the colon-syntax looks as follows.

variable: value

The value can be a computation, which directly executes.

x: 2 + 2

Then, after the definition, you can use the value by name.

cos[pi]

Own functions

You can build own functions by putting computations into curly braces.

For example you can implement the mathematical function f(x) = sin(x)%x.

f: {sin[x] % x}

And you can use it like any other function.

f[2]

However, it isn't needed to assign the function expression to a variable.

{sin[x] % x}[2]

The first argument in a function is always called x, the second one y and the third z.

g:{(2 * x) + y}
10 g 20

The function itself is called self.

/Wrong:
f:{f}
/Right:
f:{self}

The sequence of arguments is stored in args.

sequence:{args}

You can put private variables inside a function expression, too.

h:{a: x + y; b: x * y; a + b + z}

Sequences

AntLang supports a datatype called a sequence.

A sequence is a list of elements, which can be numbers, strings or other sequences.

The seq-function constructs them.

x: seq[1;2;3]

For a sequence of ordered values, i.e. a range, you can construct them using the range-function.

y: range[10]

You can do several operations with sequences.

x concat y /Concatenation
map[sin; x] /Maps
foldl[*; 1; x] /Folds (foldl/foldr)

However, there is one super function: merge. It takes some sequences and zips them by producing sequences.

merge[seq[1;2;3]; seq[4;5;6]] /=> seq[seq[1;4]; seq[2;5]; seq[3;6]]

Files

If you have AntLang code and you want to store it, you can put it in a file and reload it later.

By convention the files end with ".ant"

my-file.ant

You can read the file linewise, while each line should be a valid AntLang term using load.

load["my-file.ant"]

Further reading

If you want to program using AntLang, you now have the basics.

However, they are many functions implemented in AntLang not shown in this tutorial.

You can check them out in the documentation.

If you have questions, ask the "AntLang user group" community at google+ (you have to join to ask questions).