I am getting a recursion problem I don't understand. (gpc-20040516, LINUX on a Pentium 4 laptop.)
This is a (silly) example program (enclosed as a gzipped tar file.):
program subset_problem(input,output); const setlimit = 4; type subset = set of 0..255;
var seta, setb: subset;
function recursive_setprint(set1: subset):subset; var i : integer; found : boolean; returned : boolean;
begin if set1 <> [] then begin i:= 0; found := false; while ((i <= setlimit) and (not found)) do begin if i in set1 then begin writeln('first element: ',i); found := true; recursive_setprint := recursive_setprint(set1 - [i]); end; i := i + 1; end end end;
begin seta := [0..setlimit]; setb := recursive_setprint(seta); end.
I'm getting the following output: first element: 0 first element: 1 first element: 2 first element: 3 first element: 4 first element: 4 first element: 3 first element: 4 first element: 4 first element: 2 first element: 3 first element: 4 first element: 4 first element: 3 first element: 4 first element: 4 first element: 1 first element: 2 first element: 3 first element: 4 first element: 4 first element: 3 first element: 4 first element: 4 first element: 2 first element: 3 first element: 4 first element: 4 first element: 3 ... and so on.
I'm expecting: first element: 0 first element: 1 first element: 2 first element: 3 first element: 4 (This is the output I am getting with Digital Pascal.)
Thanks for your help with this.