
(* Generic access to array cell *)
(* Recursive linear, (lustre-v4 like) *)

function access
	<<type t; const sz: int; const dflt: t>>
	(tab: t^sz; ix: t) returns (res: t);
let
	res = with ( sz = 1) then
	        if ix = 0 then tab[0] else dflt 
	      else
	        if ix = 0 then tab[0]
	        else access<<t, sz-1, dflt>>(tab[1..sz-1], ix-1)
			;
tel

(* Recursive logarithmic, (lustre-v4 like) *)
function quick_access
	<<type t; const sz: int; const dflt: t>>
	(tab: t^sz; ix: t) returns (res: t);
const mid : int = sz div 2;
let
	res = with ( sz = 1) then
	        if ix = 0 then tab[0] else dflt 
	      else
	        if ix < mid then
	           quick_access<<t, mid, dflt>>(tab[0..mid-1], ix)
	        else
	           quick_access<<t, sz-mid, dflt>>(tab[mid..sz-1], ix-mid)
	      ;
tel
