### Another release - 0.3

This release, if you've been following the blog, introduces continuations. Everything seems to work as it's supposed to, hence another release. Just don't forget it's pretty alpha, there may be serious bugs.

Besides the code for continuations I've also added a new test exemplifying them, and I'd like to call your attention to it, as I believe it shows the great power of continuations:

`Ambivalence<Integer> amb =`

newAmbivalence<Integer>();try{

inth = amb.choose(range(1, N));

inta = amb.choose(range(1, h));

intb = amb.choose(range(a, h));

amb.require(sqr(h) == sqr(a) + sqr(b));

System.out.printf("%2d^2 + %2d^2 = %2d^2\n", a, b, h);

amb.fail();

}catch(Exception e) {

}

Let's go through it step by step.

Firstly what is the code supposed to do? Well, the goal is to find Pythagorean Triplets, that is integer (

`a`,

`b`,

`h`) triplets that satisfy the Pythagorean Theorem

`a`²+

`b`²=

`h`².

Having set its goal, the code (or at least a portion of it) is quite easy to read:

- you state that
`h`is to be chosen from a range of 1 through a given`N`; (`N`exclusively) - you state that
`a`is to be chosen from a range of 1 through`h`; (the hypotenuse is always larger than the legs) - you state that
`b`is to be chosen from a range of`a`through`h`; (making`b`be greater than or equal to`a`removes the uninteresting solutions where`a`and`b`are interchanged) - you require that
`h`squared is to be equal to the sum of`a`squared and`b`squared.

And then you go on to printing the solution. So, basically you've set out the rules and you hope the program will automagically give the right answer, and it does - that's

`amb`

. You can read about non-determinism and `amb`

on two great Scheme books: SICP and TYS.`Ambivalence`

differs from it's Scheme's cousin in that it is not a macro, and so it evaluates it's arguments, but otherwise it's pretty much the same. Modulo that difference, it can be used in almost the same ways. It's code is part of the test, but it may be added to the general library, as a clean way to use continuations. I'll post it here for a good showcase, anyway:

publicfinalclassAmbivalence<T> {

privateContinuation failure;

public@interpretable T choose(T... values) {

if(values.length == 0) failure.returnTo();

if(values.length == 1)returnvalues[0];

Continuation old = failure;

Continuation success =newContinuation();

for(T value : values) helper(value, success);

failure = old;

failure.returnTo();

returnnull;

}

private@interpretablevoidhelper(T value, Continuation success) {

failure =newContinuation();

success.returnTo(value);

}

public@interpretablevoidfail() {

failure.returnTo();

}

public@interpretablevoidrequire(booleancondition) {

if(!condition) failure.returnTo();

}

}

That's it for today. I'm working on

`finally`

and `continuing`

blocks, and hope to nail them soon. Keep tuned.
## No comments:

Post a Comment