home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / ada / 2170 < prev    next >
Encoding:
Internet Message Format  |  1992-07-22  |  2.8 KB

  1. Path: sparky!uunet!mcsun!uknet!warwick!dcs.warwick.ac.uk!sunserver1.aston.ac.uk!cs_mac35.aston.ac.uk!user
  2. From: elsworthef@aston.ac.uk (Ted Elsworth)
  3. Newsgroups: comp.lang.ada
  4. Subject: Re: Shortest Self-Generating Ada Program
  5. Message-ID: <elsworthef-220792115757@cs_mac35.aston.ac.uk>
  6. Date: 22 Jul 92 11:09:01 GMT
  7. Sender: usenet@aston.ac.uk (Usenet administrator)
  8. Followup-To: comp.lang.ada
  9. Organization: Computer Science, Aston University
  10. Lines: 77
  11. Nntp-Posting-Host: cs_mac35
  12.  
  13. This was my *must-be-as-one-line* Ada self-replicating program:
  14.  
  15. with text_io;use text_io;procedure r is s:string(1..115):=
  16. "with text_io;use text_io;procedure r is s:string(1..115):="";
  17. begin put(s(1..59)&s(1..59)&s(59..115)&s(59..115));end;";
  18. begin put(s(1..59)&s(1..59)&s(59..115)&s(59..115));end;
  19.  
  20. If you or your compiler don't like the above, here is another (57-line)
  21. self-replicating program with maximum line length 78 chars 
  22. and using (hopefully) reasonably clear layout and identifiers.
  23. Also, it doesn't need the programmer to do any character counting.
  24.  
  25. Ted Elsworth.
  26.  
  27. ===================================================================
  28.  
  29. with text_io; use text_io;
  30. procedure rep is
  31.   s : constant string :=
  32. "with text_io; use text_io;\"&
  33. "procedure rep is\"&
  34. "  s : constant string :=\"&
  35. """\"&
  36. "  procedure put_s(pass : integer) is\"&
  37. "    skipping_part1 : boolean := pass=3 or pass=4;\"&
  38. "  begin\"&
  39. "    for i in 1..s'last loop\"&
  40. "      if s(i) = ascii.quotation then skipping_part1 := false; end if;\"&
  41. "      if not skipping_part1 then\"&
  42. "        if s(i) = ascii.back_slash then\"&
  43. "          if pass=2 or pass=3 then\"& 
  44. "         
  45. put(ascii.back_slash);put(ascii.quotation);put(ascii.ampersand);\"& 
  46. "          end if;\"&
  47. "          new_line;\"&
  48. "          if pass=2 or pass=3 then put(ascii.quotation); end if;\"&
  49. "        else\"&
  50. "          put(s(i));\"&
  51. "        end if;\"&
  52. "        if s(i)=ascii.quotation and (pass=1 or pass=2) then exit; end
  53. if;\"&
  54. "      end if;\"&
  55. "    end loop;\"&
  56. "  end;\"&
  57. "begin\"&
  58. "  put_s(1);\"&
  59. "  put_s(2);\"&
  60. "  put_s(3);\"&
  61. "  put_s(4);\"&
  62. "end;";
  63.   procedure put_s(pass : integer) is
  64.     skipping_part1 : boolean := pass=3 or pass=4;
  65.   begin
  66.     for i in 1..s'last loop
  67.       if s(i) = ascii.quotation then skipping_part1 := false; end if;
  68.       if not skipping_part1 then
  69.         if s(i) = ascii.back_slash then
  70.           if pass=2 or pass=3 then 
  71.           put(ascii.back_slash);put(ascii.quotation);put(ascii.ampersand); 
  72.           end if;
  73.           new_line;
  74.           if pass=2 or pass=3 then put(ascii.quotation); end if;
  75.         else
  76.           put(s(i));
  77.         end if;
  78.         if s(i)=ascii.quotation and (pass=1 or pass=2) then exit; end if;
  79.       end if;
  80.     end loop;
  81.   end;
  82. begin
  83.   put_s(1);
  84.   put_s(2);
  85.   put_s(3);
  86.   put_s(4);
  87. end;
  88.  
  89. =============================================================================
  90.