An algorythm?(+) Please, could you shortly tell me an algo of a introspective program? In any language:-) Me also. An algorithm for any language? Posted by asif 14 Nov 2002 14:51 Example of an introspective program on Pascal (+) Here is my variant of an introspective program. It is written on Pascal. I think it will not be hard to convert it to C/C++ But I do not think it is easy make same under PIBAS, because PIBAS does not have for/while cycles :(. Some explanations: 1. Every const string (Stgs[ 2] := '...') Has " symbol not ' because Pascal needs doubled '' to mark single ' inside strings. 2. Chr(34) = ' 3. Chr(39) = " ----- Cut here ----- program IntrospectiveProgram; var Stgs: array[1..20] of String; i, j: Integer; begin Stgs[ 1] := ''; Stgs[ 2] := ' WriteLn("program IntrospectiveProgram;");'; Stgs[ 3] := ' WriteLn("var");'; Stgs[ 4] := ' WriteLn(" Stgs: array[1..20] of String;");'; Stgs[ 5] := ' WriteLn(" i, j: Integer;");'; Stgs[ 6] := ' WriteLn;'; Stgs[ 7] := ' WriteLn("begin");'; Stgs[ 8] := ''; Stgs[ 9] := ' for i := 1 to 20 do'; Stgs[10] := ' WriteLn(" Stgs[",i:2,"] := """, Stgs [i], """;");'; Stgs[11] := ' WriteLn;'; Stgs[12] := ''; Stgs[13] := ' for i := 1 to 20 do'; Stgs[14] := ' for j := 1 to Length(Stgs[i]) do'; Stgs[15] := ' if Stgs[i][j] = Chr(34) then'; Stgs[16] := ' Stgs[i][j] := Chr(39);'; Stgs[17] := ''; Stgs[18] := ' for i := 1 to 20 do'; Stgs[19] := ' WriteLn(Stgs[i]);'; Stgs[20] := 'end.'; WriteLn('program IntrospectiveProgram;'); WriteLn('var'); WriteLn(' Stgs: array[1..20] of String;'); WriteLn(' i, j: Integer;'); WriteLn; WriteLn('begin'); for i := 1 to 20 do WriteLn(' Stgs[',i:2,'] := ''', Stgs[i], ''';'); WriteLn; for i := 1 to 20 do for j := 1 to Length(Stgs[i]) do if Stgs[i][j] = Chr(34) then Stgs[i][j] := Chr(39); for i := 1 to 20 do WriteLn(Stgs[i]); end. ---- And here ---- Thank you very much. > Here is my variant of an introspective program. It is written on > Pascal. I think it will not be hard to convert it to C/C++ > But I do not think it is easy make same under PIBAS, because > PIBAS does not have for/while cycles :(. > > Some explanations: > 1. Every const string (Stgs[ 2] := '...') Has " symbol not ' > because Pascal needs doubled '' to mark single ' inside strings. > 2. Chr(34) = ' > 3. Chr(39) = " > > ----- Cut here ----- > program IntrospectiveProgram; > var > Stgs: array[1..20] of String; > i, j: Integer; > > begin > Stgs[ 1] := ''; > Stgs[ 2] := ' WriteLn("program IntrospectiveProgram;");'; > Stgs[ 3] := ' WriteLn("var");'; > Stgs[ 4] := ' WriteLn(" Stgs: array[1..20] of String;");'; > Stgs[ 5] := ' WriteLn(" i, j: Integer;");'; > Stgs[ 6] := ' WriteLn;'; > Stgs[ 7] := ' WriteLn("begin");'; > Stgs[ 8] := ''; > Stgs[ 9] := ' for i := 1 to 20 do'; > Stgs[10] := ' WriteLn(" Stgs[",i:2,"] := """, Stgs > [i], """;");'; > Stgs[11] := ' WriteLn;'; > Stgs[12] := ''; > Stgs[13] := ' for i := 1 to 20 do'; > Stgs[14] := ' for j := 1 to Length(Stgs[i]) do'; > Stgs[15] := ' if Stgs[i][j] = Chr(34) then'; > Stgs[16] := ' Stgs[i][j] := Chr(39);'; > Stgs[17] := ''; > Stgs[18] := ' for i := 1 to 20 do'; > Stgs[19] := ' WriteLn(Stgs[i]);'; > Stgs[20] := 'end.'; > > > WriteLn('program IntrospectiveProgram;'); > WriteLn('var'); > WriteLn(' Stgs: array[1..20] of String;'); > WriteLn(' i, j: Integer;'); > WriteLn; > WriteLn('begin'); > > for i := 1 to 20 do > WriteLn(' Stgs[',i:2,'] := ''', Stgs[i], ''';'); > WriteLn; > > for i := 1 to 20 do > for j := 1 to Length(Stgs[i]) do > if Stgs[i][j] = Chr(34) then > Stgs[i][j] := Chr(39); > > for i := 1 to 20 do > WriteLn(Stgs[i]); > end. > ---- And here ---- Here's a C Quine :-) char *f = "char *f = %c%s%c; void main(){printf(f, 34, f, 34, 10);%c" void main() { printf(f, 34, f, 34, 10); } Another idea The main point of my solution is, firstly, to become free of constant non-variable strings in output and, secondly, to use substring operator to replicate print. Let's define double quote as D, and single quote as S, sembol of endline as E: {{{ D='"';S="'";E=";" }}} After defining that, we need to print that string. Let's use support string C: {{{ C="C=D=S=E="; ... ?$(C,1,2)+D+C+D+E #printed definition of C +$(C,3,2)+S+D+S+E #printed definition of D ... #print the rest alike }}} But how we'll print this particular "?" operation? Let's append all that operator body to our support string C, and then print it's substring, which will represent needed operator: {{{ C="C=D=S=E=?$(C,1,2)+D+C+D+E+$(C,3,2)+S+D+S+E+$(C,5,2)+D+S+D+E+$(C,7,2)+D+E+D+E+$(C,9,78)"; }}} You can simply prove by yourself, that this substring is 78 symbols long. Thus, solution found. My full PIBAS code is 187 lines long. Edited by author 05.11.2011 03:44 Edited by author 05.11.2011 03:44 |