Wednesday, May 27, 2009

Lambda Templates

I have just putted this piece of code into the repository together with small wiki article.

LambdaTemplates
Building a very trivial tool for programmable templates support

Background

Template techniques are widely used in programming languages and, of course, the web. This small piece of code that I have "cleverly" called Lambda Templates is yet another string substitution tool (as I have found out later initially very similar to String Templates by Terrence Parr).

Usually, templates mechanism tries to follow procedural paradigm of text generation by simple replacing of a bunch of specially marked placeholders with passed values. For my needs, I wanted to have something very short at hand: to call a method and process the result further within the program code.

Examples

To set a constant string value to some variable, one calls $p->setVar('foo','bar');. Apparently, $p->getVar('foo'); will return the corresponding value.

Now the name Lambda featuring the title, comes from Lambda calculus by Alonzo Church, where one can observe a sort of substitution mechanism for resolution of specially build logical expression. Well, actually the resemblance ends at the point of using "/" that mimics lambda symbol in lambda expressions.

Once again, the idea behind a lambda template is actually as trivial as this: /fruit. this is a fruit - an example of parametrized template's value. Because it's inspired by lambda calculus, we don't give a name for this expression inside the value. The comma separated list of argument names between LAMBDA and DOT symbols, are actually names that will be reserved for substitution after DOT.

Grammar will be something like

expression := body | LAMBDA (argument COMMA)* DOT body;
body
:= CHAR*;

Further examples are:

$p = new LambdaTemplates();
$p
->setVar('x','10');
$p
->setVar('what is x','/x.is /x = x?');
echo $p
->processTemplate('question: %what is x%');

$p
= new LambdaTemplates();
$p
->setVar('titel','herr');
$p
->setVar('name','y.y.');
$p
->setVar('titelname','/titel,name.titel name');
echo $p
->processTemplate('question: %titelname%');

Global scope

Current implementation does not support a proper functional paradigm for templating with closure and application constructions (and maybe proper reduction). All parametrized names inside argument list are applied globally, that is extracted from the global scope of values and substituted to the expression.

Friday, May 22, 2009

light bot

Great game in Action Script. Maybe the simplest way to teach basic algorithm development skills ever.

Drag and drop actions (icons) into the sequence to create a small program.

Your goal is to light the lamp over special blue areas.

And "programming" of the game can go pretty sophisticated, that is functional (see the following picture):



Idea behind is pretty cool, although not new. There are a lot of cases where games exploit mechanical scenarios and achieve amazing resembles of certain automaton model with questionable equivalence to some Turing Machine.

Wednesday, May 13, 2009

meditation cite

Meditation brings wisdom; lack of meditation leaves ignorance. Know well what leads you forward and what holds you back, and choose the path that leads to wisdom.