# fun with generators link graphics procedure main() Window := WOpen("size=500,270") | stop("*** cannot open window") WWrite(Window," Fun With Generators!") # using the every expression with the 'to' generator WWrites(Window,"\nevery: Enter a string: ") # 'every' executes the line until it fails # by backtracking the expression str := WRead(Window) # 0 to *str generates the numbers between every i:= 1 to *str do # one and the length of the string str WWrites(Window,str[i]) # incrimenting by 1 each time it is called # then executing the WWrites expression WWrites(Window,"\nto-by: Enter a string: ") # this time the to-by generator is used str := WRead(Window) # and it can incriment in the negative every WWrites(Window,str[*str to 1 by -1]) # direction granted that str > 0 WWrites(Window,"\n!str: Enter a string: ") # the element generator !e str := WRead(Window) # for strings generates one character every WWrites(Window,!str) # substrings eqivalent to x[1 to *x] # other ways to induce backtracking other than 'every' # the &fail expression always fails WWrites(Window,"\n&fail: Enter a string: ") # causing the system to backtrack str := WRead(Window) # and see if it can generate a value WWrites(Window,str[1 to *str]) + &fail # that will allow it to succeed. # arithmatic generators WWrites(Window,"\nEnter a number: ") num1 := WRead(Window) WWrites(Window,"Enter another number: ") num2 := WRead(Window) num1 := num1 < num2 # relational operator '<' can be used # to generate the greater of the two # values. it returns num2 if true # and fails if false exiting the expression WWrites(Window,"num1:=",num1,"<",num2," returns: ",num1,"\n") # conjuction '&' with generators every i:=1 to num1 & j:=1 to num2 & writes(Window,i+j) # & evaluates each generator in sequence # and ends when all the generators fails WWrites(Window,"\n") # Alternation e1|e2 every i:= 1|2|3|4 do WWrites(Window,i) # | or evaluates for each case # before failing WWrites(Window,"\n") # Sequence generation seq(...) WWrites(Window,seq(1,10)) # seq works like 'to' except it can # generate infinite numbers by typing # seq() or seq(i) which starts at # zero or i and counts up WRead(Window) end