Wednesday, July 23, 2008

X Erlang shell application

To demonstrate X Erlang system I quickly put together a simple web server which serves static files and runs scripts. On top of it I implemented a simple shell application which allows to enter and evaluate arbitrary Erlang expressions from a web browser window. First I put the system running on an Amazon EC2 server but then I put it down after all my friend had a look at how it works.

The typical shell session looks like this:

X Erlang v0.2 is here, type '?' for help

> test:primes(100).
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73, 79,83,89,97]


> [{X,Y} X <- test:primes(100), Y <- test:primes(100), Y =:= X+2].
[{3,5}, {5,7}, {11,13}, {17,19}, {29,31}, {41,43}, {59,61}, {71,73}]


> code:all_loaded().
[lists,test,dict,orddict,queue,sets,erl_parse,regexp,gen_tcp,inet,io_lib_format,erl_lint,dashboard,stdio,gb_sets, io_lib,erlang,io,packages,io_lib_pretty,prim_erlang,code,erl_eval,string,init,erl_internal,files,ordsets,efile, erl_scan,spur,error_handler,x_internal]


$_

The blue characters are entered by the user, the rest is the output of the system. The first
request is to calculate a few prime numbers. Every programming language is demonstrated using prime number routine. Why should I be different? The second request is trickier. The entered expression finds all 'twin primes' less than 100. As you noticed X Erlang supports list comprehensions (but not yet binary comprehensions). The last expression is a maintenance job. It shows which modules are loaded.

Nothing to fancy but it works. It took me a day or two to program a robust shell application on top of a web server. It may take even less for other programmers using other versions of Erlang.

1 comment:

Unknown said...

Max, this looks like a great starting point for having something like the interactive Ruby tutorial for Erlang. That would be an absolutely awesome thing to have. Have you posted the X Erlang shell source to somewhere like GitHub where I can pull it down and play with it?