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 / versum.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  76 lines

  1. ############################################################################
  2. #
  3. #    File:     versum.icn
  4. #
  5. #    Subject:  Program to produce versum sequence 
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     August 12, 1995
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program writes the versum sequence for an integer to a file of a
  18. #  specified name.  If such a file exists, it picks up where
  19. #  it left off, appending new values to the file.
  20. #
  21. #  The supported options are:
  22. #
  23. #    -s i    The seed for the sequence, default 196
  24. #    -f s    Name of file to extend, no default
  25. #    -F s    Name of file, default <i>.vsq, where <i> is the
  26. #           seed of the sequence
  27. #    -t i    The number of steps to carry the sequence out to, default
  28. #           essentially unlimited
  29. #    -m i    Stop when value equals or exceeds m; default no limit
  30. #
  31. #  If both -f and -F are given, -f overrides.
  32. #
  33. ############################################################################
  34. #
  35. #  Links:  options
  36. #
  37. ############################################################################
  38.  
  39. link options
  40.  
  41. procedure main(args)
  42.    local start, output, input, i, opts, limit, name, max, count
  43.  
  44.    opts := options(args, "t+s+m+f:F:")
  45.    start := (0 < \opts["s"]) | 196
  46.    limit := \opts["t"] | -1
  47.    max := opts["m"]
  48.    name := \opts["F"] | (start || ".vsq")
  49.    name := \opts["f"]
  50.   
  51.    if input := open(name) then {
  52.       count := 0
  53.       while i := read(input) do {
  54.           if not integer(i) then exit()            # link, not term
  55.          count +:= 1
  56.          if count > limit then exit()
  57.          }
  58.       close(input)
  59.       }
  60.  
  61.    /i := start                # in case file doesn't exist or is empty
  62.  
  63.    if not integer(i) then stop("*** invalid data")
  64.  
  65.    output := open(name, "a") | stop("*** cannot open file")
  66.  
  67.    limit -:= \count
  68.  
  69.    until (limit -:= 1) = -1 do {
  70.       i +:= reverse(i)
  71.       if i > \max then break
  72.       write(output, i := string(i))
  73.       }
  74.  
  75. end
  76.