Annotation of 43BSD/contrib/icon/src/cmd/parens.icn, revision 1.1.1.1

1.1       root        1: #      PARENS(1)
                      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:    every 1 to limit do {
                     29:       if \varying then k := 2 * ?bound else k := 2 * bound
                     30:       string := ""
                     31:       r := 0
                     32:       while k ~= r do {
                     33:          if r = 0 then string ||:= Open()
                     34:          else if ?0 < probClose()
                     35:             then string ||:= Close() else string ||:= Open()
                     36:          }
                     37:       while k > 0 do string ||:= Close()
                     38:       write(string)
                     39:       }
                     40: end
                     41: 
                     42: procedure Open()
                     43:    r +:= 1
                     44:    k -:= 1
                     45:    return lp
                     46: end
                     47: 
                     48: procedure Close()
                     49:    r -:= 1
                     50:    k -:= 1
                     51:    return rp
                     52: end
                     53: 
                     54: procedure probClose()
                     55:    return ((r * (r + k + 2)) / (2.0 * k * (r + 1)))
                     56: end
                     57: 
                     58: procedure Usage()
                     59:    stop("usage: parens [-n i -b i -l s -r s]")
                     60: end

unix.superglobalmegacorp.com

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