/AntLang Blog


June 3 (2016)
ALC Report
May 28 (2016)
The future of AntLang
May 23 (2016)
AntLang in practice
How to tweet a program
May 22 (2016)
The merge function
AntLang code repository
Index of element in sequence
Latest AntLang Patch
May 21 (2016)
Point-free style programming
The fibonacci sequence
AntLang on RosettaCode

ALC Report

The ALC (AntLang Compiler) is currently under active development.

Some very cool decisions were felt during development.

One of them is that the ALC is written in AntLang itself and can be executed using an other working AntLang implementation.

Another cool benefit is that it can be understood by any AntLang programmer, cause it is written using easy to understand constructs - no fancy stuff and no jacking with the main implementation.

If you want to help developing or just want to take a look at the compiler, you can check out its source code on GitHub.

The future of AntLang

In these days, the AntLang interpreter is written in Haskell, a nice functional language.

For now, Haskell is fast enough, but the AntLang project grows and having a slow tool is not what the community wants.

Currently AntLang is rewritten from scratch, but this time it will be implemented as a compiler.

The new AntLang version will be completely compatible, so you can switch without rewriting existing code.

Compiled code basically runs faster than interpreted code, but with the cost of interactivity.

Therefore the AntLang repl, written in Haskell will be maintained, too.

The AntLang compiler will be written in AntLang itself and it will generate C-Code.

However, the AC (AntLang compiler) is still in the design stage, so things can change.

You can help developing the compiler, if you want to.

For example, you can write the lexer/scanner (You can find inspiration at the AntLang repl source code on github).

AntLang in practice

AntLang is a powerful tool, but sometimes using it in practice seems inconvenient.

However, AntLang can be used for many things.

For example some pages on this site, like the code page are automatically generated using index.ant.

The script collects all files in a directory and creates an index.html file which links to them.

I recommend reading the file to any AntLang programmer, because it shows how to use the language in practice.

Another auto-generated file is the documentation.

If you have programmed something similar in AntLang, please publish it, so that everybody can profit from your work.

How to tweet a program

In this post we discuss how to make extremely short programs using AntLang.

We hack around with the lexer/scanner to remove as much whitespace as possible.

Say we have the following program.

x : { ( 1 + log [ 2 ; 10 ] ) % x } [ 3 ]

In AntLang basically any token (syntactical unit) is surrounded with whitespace.

However, we need no whitespace around parenthesis.

x : {(1 + log[2 ; 10])% x}[3]

Much shorter, isn't it?

We can also leave whitespace around semicolons.

x : {(1 + log[2;10])% x}[3]

We need no space around colons, too.

x:{(1 + log[2;10])% x}[3]

And now the last trick: we need no whitespace after a number.

x:{(1+ log[2;10])% x}[3]

The merge function

AntLang comes with many useful functions and one of them is extremely powerful.

The magic power of the so called merge function isn't that obvious.

For those who don't know what it does, it groups corresponding elements.

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

The function allows us to operate on corresponding elements easily.

For example, here is a function that implements vector addition.

v+:{map[{+ apply x}; merge[x; y]]} /No amend: "v+" is the function name

It adds two sequences element-wise.

seq[1; 2; 3] v+ seq[4; 5; 6] /=> seq[5; 7; 9]

This should demonstrate why merge is so powerful: it allows us to operate element-wise.

Another interesting fact is that merge also is an involutory function.

f:{merge apply x}
f[f[x]] eq x

If you like AntLang, please join the "AntLang user group" on google+.

You can ask questions there and talk to other AntLang programmers (The community is new, so don't worry if there are only a few users).

AntLang code repository

AntLang code can be very interesting and it makes you a better programmer to look at solutions others have found.

Maybe someone else has solved a problem you are currently facing.

For this reason AntLang got a code repository!

The code there is released under the UNLICENCE and therefore can be used for nearly everything!

If you know AntLang or are interested in how it looks, check it out!

Index of element in sequence

Many programming languages provide a built-in function to find out where in a sequence an element occurs.

The version of AntLang on May 22 (2016) has no bif for this purpose, but we can make one on our own.

First, the basic construct of the function.

index: {sequence: x; element: y; ..}

We can generate a sequence of integers (from 0) to know which index corresponds to which element.

range[length[sequence]]

We also have to create a boolean sequence to check if the element is the one we are looking for or not.

map[{element eq x}; sequence]

Now merge the sequences together.

map[{element eq x}; sequence] merge range[length[sequence]]

The new sequence lists the indexes and also contains information about the content (element or not).

We can filter out the ones, starting with a boolean 1, because they contain the indices we need.

{0 elem x} hfilter map[{element eq x}; sequence] merge range[length[sequence]]

Note, that we can write {1 eq 0 elem x}, but this version is more compact, because we already have boolean values.

Now every pair in our sequence starts with a 1, but we are only interested in the second element (start from 0 -> 1st element)

{1 elem x} map {0 elem x} hfilter map[{element eq x}; sequence] merge range[length[sequence]]

We can wrap the solution in our function to reuse it later.

index: {sequence: x; element: y; {1 elem x} map {0 elem x} hfilter map[{element eq x}; sequence] merge range[length[sequence]]}

Latest AntLang Patch

This morning I patched the executable and added 2 functions.

If you need one of them, feel free to download AntLang again, it is backward compatible.

In this blog post I introduce both functions and why I decided to add them.

The first one is the `arguments'-function.

It is arity 0 and returns a sequence of command line arguments.

This function only works inside of scripts, therefore you can not use it interactively.

The sequence contains only strings, so if you need numbers from the console you have to convert (for example using `eval').

The second function is the `wread'-function.

Consider the following problem: You want to work with some kind of data, but it isn't on you local machine.

Before this patch, you had to download the data using curl or something like this, but now AntLang comes with a built-in way to do so.

For example, the following code snippet downloads the content of this website.

wread["http://ac1235.github.io/blog.html"]

I recommend downloading the newest AntLang version, because it is just one file and the new features should be interesting for scientists (extract data from website) and programmers (extract data from command-line).

The next release includes the same functionality, but for a different operating system... Stay tuned!

Point-free style programming

Point-free style is constructing a function only out of other functions, without named arguments, etc.

AntLang2000 (the next version) may supports built-in facilities for this, but for now, we have to build our own functions for this.

One very common function is the composition function.

(f[g[h[x; y]]]) eq compose[f; g; h][x; y]

Using this function, we can build the following function more easy.

f: {-[x + y]}

Using composition it looks like this.

f: compose[-; +]

We can build the composition function in AntLang using a fold.

compose: {f: x; fs: tail[args]; {f apply foldr[{seq[y apply x]}; args; fs]}}

Another useful function, let's call it the train function, can simplify the following term.

{f[g[x]; h[x]]} --> train[f; g; h]

Using trains, we can implement a tangent function.

tan': train[%; sin; cos]

We can implement train by mapping the last functions over the arguments and then applying the first one.

train: {f: x; fs: tail[args]; {xs: args; f apply {x apply xs} map fs}}

The fibonacci sequence

The fibonacci sequence is very interesting from a mathematical point of view.

In this blog post, I try to reconstruct it using the AntLang programming language.

I recommend reading this post to every AntLang newcomer, because it not only shows how to code in AntLang, it shows how to think in it.

OK, so let's get started. The fibonacci numbers can be built on top of two start numbers.

start: seq[0; 1]

Then it can be extended, by adding up the last two elements.

0, 1, 1, 2, 3, 5, 8, ...

The extend function appends one element to it.

extend: {x concat seq[(-1 elem x) + (-2 elem x)]}
extend[extend[start]] /=> seq[0; 1; 1; 2]

To extend it y-times, we can fold over a sequence with length y.

extendy: {foldl[extend; x; range[y]]}
extendy[start; 2] /=> seq[0; 1; 1; 2]

To provide a single-function interface, we can strip down the whole thing to one line.

fibonacci: {foldl[{x concat seq[(-1 elem x) + (-2 elem x)]}; seq[0; 1]; range[x]]}
fibonacci[5] /=> seq[0; 1; 1; 2; 3; 5; 8]

And were done!

I hope this helps you to see how to think in AntLang.

If you have questions, you can send me an email (anthony.nc[at]web.de).

AntLang on RosettaCode

AntLang is a powerful tool for solving all kinds of problems.

The information on the home page helps you to learn AntLang, but if you already know the basic language, it makes you a better programmer to simply look at real code.

Therefore, AntLang joined RosettaCode!

RosettaCode is a page listing different tasks and corresponding solutions in many programming languages.

The AntLang page on RosettaCode is a nice place to increase your AntLang skills.

However, they are only a few problems solved in AntLang, so you can help!

You can join RosettaCode and solve problems using AntLang programming language.

PS: RosettaCode is a great wiki, even if you aren't skilled enough to solve tasks, I recommend you to just look at solutions others have found.