67

Lang5 – A Stack Based Array Language

 6 years ago
source link: https://www.tuicool.com/articles/hit/rqmqm2y
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What is lang5?

Lang5 is a stack based array language. What does that mean? First of all, it means that lang5 is maybe quite different from other programming languages you may already know.

OK, so how do lang5 programs look like? Let's have a look at a simple example. You all know the story of little Gauss in school when he was asked to calculate the sum of the first one hundred integers 1, 2, ..., 100. Being a genius as he was, he quickly reached the conclusion that the result must be 5050. If you would do such a calculation (without employing the Gauss-formula for cases like this) in a traditional language like C, you might write a program like this:

Sum of 1, 2, ... 100 in C

#include <stdio.h>

int main()
{
  int i, sum = 0;

  for (i = 1; i < 101; sum += i++);
  printf("Result: %d\n", sum);
}

Obviously this program contains a lot of things that deal with the computer and not the problem of calculating a sum. The very same problem can be solved using lang5 like this:

Sum of 1, 2, ... 100 in lang5

100 iota 1 + '+ reduce .

Did you note the main difference (apart from the fact that the lang5 solution is much shorter than the C program)? We don't need an explicit loop to compute this sum in lang5. But... How does this work?

  • Since lang5 is stack oriented, we first push the value 100 onto the stack by just writing 100
  • The iota function expects a positive integer on the stack and returns a vector with as many elements as specified by this value. The vector elements are an arithmetic progression, so 100 iota yields a vector 0 1 2 3 .. 99 .
  • Since this vector starts by 0 and ends at 99 we have to add 1 to every vector element. This is simply done by pushing 1 onto the stack and adding it to the vector: 1 + . Now we have a vector 1 2 3 4 .. 100 on the stack.
  • Next we push an operator, + , onto the stack and execute the function reduce . This function applies a binary operator like the addition we use here between every two elements of a vector and evaluates the resulting expression which is 1+2+3+4+..+100 .
  • The final dot in the program above prints the element on the stack which is just the sum we were looking for.

If these ideas remind you of APL, the programming language devised by Ken Iverson, you are right. lang5 owes a lot to APL and to Forth, to be exact.

Why would one use lang5 and not APL or Forth? A couple of reasons may come to mind:

  • lang5 is open source and you can not only use the language but join the development team, implement your own functionalities etc.
  • lang5 does not need APL's special character set (which is still quite a challenge even given modern GUIs).
  • lang5 is written in pure Perl and runs on a wide variety of platforms. Currently we see lang5 interpreters running on Mac OS X, Windows, several LINUX distributions and even OpenVMS systems just out of the box.
  • lang5 is really small. The distributin kit is only about 400 kB in size and all you need on your target machine is a Perl interpreter (5.8.x or newer, although lang5 should work with older Perl interpreters, too).

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK