[Shootout-list] Many tests cheat

skaller skaller@users.sourceforge.net
03 Nov 2004 15:55:02 +1100


On Wed, 2004-11-03 at 12:23, Isaac Gouy wrote:

> *Underneath* 
> Wrapped up with try & except & raise

The current Felix code looks like this:
----------------------------------------
// Felix uses explicitly passed closures to handle errors
// and therefore guarrantees exceptions get caught statically

typedef err = int -> void;

var hi = 0;
var lo = 0;

proc blowup (n:int, hie:err, loe:err) {
  if n % 2 == 0 do loe n; else hie n; done;
}

proc lo_fun (n:int, hie:err) {
  blowup (n,hie,loe of (int));
  proc loe(n:int) { ++lo; goto fin; }
fin:>
}

proc hi_fun (n:int) {
  lo_fun (n,hie of (int));
  proc hie(n:int) { ++hi; goto fin; }
fin:>
}

proc some_fun (n:int) { hi_fun n; }

var i = n - 1;
until i < 0 do some_fun i; --i; done;
print "Exceptions: HI="; print hi; print " / LO="; print lo; endl;
----------------------------------

Would the code be more acceptable if it looked like this:

----------------------------------------
typedef err = int -> void;

var hi = 0;
var lo = 0;

proc blowup (n:int, hie:err, loe:err) {
  if n % 2 == 0 do raise loe n; else raise hie n; done;
}

proc lo_fun (n:int, hie:err) {
  blowup (n,hie,loe of (int));
  except loe(n:int) { ++lo; }
}

proc hi_fun (n:int) {
  lo_fun (n,hie of (int));
  except hie(n:int) { ++hi; }
}

proc some_fun (n:int) { hi_fun n; }

var i = n - 1;
until i < 0 do some_fun i; --i; done;
print "Exceptions: HI="; print hi; print " / LO="; print lo; endl;
----------------------------------

In particular, this uses 'raise' to raise an exception,
and 'except' to specify one (note the 'goto' is gone).



-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net