home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / miu.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  81 lines

  1. ############################################################################
  2. #
  3. #    File:     miu.icn
  4. #
  5. #    Subject:  Program to generate strings from MIU system
  6. #
  7. #    Author:   Cary A. Coutant, modified by Ralph E. Griswold
  8. #
  9. #    Date:     January 3, 1994
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #     This program generates strings from the MIU string system.
  18. #
  19. #     The number of generations is determined by the command-line argument.
  20. #  The default is 7.
  21. #
  22. #  Reference:
  23. #
  24. #     Godel, Escher, and Bach: an Eternal Golden Braid, Douglas R.
  25. #  Hofstadter, Basic Books, 1979. pp. 33-36.
  26. #
  27. ############################################################################
  28.  
  29. procedure main(arg)
  30.    local count, gen, limit
  31.  
  32.    limit := integer(arg[1]) | 7
  33.    gen := set(["MI"])
  34.  
  35.    every count := 1 to limit do {
  36.       gen := nextgen(gen)
  37.       show(count,gen)
  38.       }
  39.  
  40. end
  41.  
  42. # show - show a generation of strings
  43.  
  44. procedure show(count,gen)
  45.  
  46.    write("Generation #",count,", ",*gen," strings")
  47.    every write("   ",image(!sort(gen)))
  48.    write()
  49.  
  50. end
  51.  
  52. # nextgen - given a generation of strings, compute the next generation
  53.  
  54. procedure nextgen(gen)
  55.    local new
  56.  
  57.    new := set()
  58.    every insert(new,apply(!gen))
  59.    return new
  60.  
  61. end
  62.  
  63. # apply - produce all strings derivable from s in a single rule application
  64.  
  65. procedure apply(s)
  66.  
  67. # Here's a case where referring to the subject by name inside scanning
  68. # is justified.
  69.  
  70.    s ? {
  71.      if ="M" then suspend s || tab(0)
  72.      tab(-1)            # to last character
  73.      if ="I" then suspend s || "U"
  74.      tab(1)            # back to the beginning
  75.      suspend tab(find("III")) || (move(3) & "U") || tab(0)
  76.      tab(1)            # back to the beginning
  77.      suspend tab(find("UU")) || (move(2) & tab(0))
  78.      }
  79.  
  80. end
  81.