Annotation of researchv10dc/cmd/icon/ipl/source/parens.icn, revision 1.1.1.1

1.1       root        1: #      PARENS
                      2: #
                      3: #      Produce parenthesis-balanced strings
                      4: #
                      5: #      Ralph E. Griswold
                      6: #
                      7: #      Last modified 8/11/84
                      8: #
                      9: 
                     10: global r, k, lp, rp
                     11: 
                     12: procedure main(a)
                     13:    local string, i, s, bound, limit, varying
                     14:    bound := limit := 10                        # default bound and limit
                     15:    lp := "("                           # default left paren
                     16:    rp := ")"                           # default right paren
                     17:    i := 0
                     18:    while s := a[i +:= 1] do
                     19:       if s[1] == "-" then
                     20:          case s of {
                     21:             "-b":   bound := integer(a[i +:= 1]) | Usage()     # bound
                     22:             "-n":   limit := integer(a[i +:= 1]) | Usage()     # total number
                     23:             "-l":   lp := a[i +:= 1] | Usage()                 # left paren
                     24:             "-r":   rp := a[i +:= 1] | Usage()                 # right paren
                     25:             "-v":   varying := 1                               # varying length
                     26:             default: Usage()
                     27:             }
                     28:       else Usage()
                     29:    every 1 to limit do {
                     30:       if \varying then k := 2 * ?bound else k := 2 * bound
                     31:       string := ""
                     32:       r := 0
                     33:       while k ~= r do {
                     34:          if r = 0 then string ||:= Open()
                     35:          else if ?0 < probClose()
                     36:             then string ||:= Close() else string ||:= Open()
                     37:          }
                     38:       while k > 0 do string ||:= Close()
                     39:       write(string)
                     40:       }
                     41: end
                     42: 
                     43: procedure Open()
                     44:    r +:= 1
                     45:    k -:= 1
                     46:    return lp
                     47: end
                     48: 
                     49: procedure Close()
                     50:    r -:= 1
                     51:    k -:= 1
                     52:    return rp
                     53: end
                     54: 
                     55: procedure probClose()
                     56:    return ((r * (r + k + 2)) / (2.0 * k * (r + 1)))
                     57: end
                     58: 
                     59: procedure Usage()
                     60:    stop("usage: parens [-v -n i -b i -l s -r s]")
                     61: end

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.