Programming Corner from Icon Newsletter 31


September 15, 1989; Icon Version 7

We have two contributions from readers this time. Rich Clayton sent the following note:
My Icon programs often are written as a series of filters on objects in a stream. The filters do one object look-ahead on the stream with a read/pushback sequence, implemented something like this:
   global push_back_o

   procedure next_o()
      local o

      if \push_back_o then {
         o := push_back_o
         push_back_o := &null
         return o
         }
      else return read_o()

      end      # next_o

It eventually occurred to me that I could use the local variable's &null initialization to rewrite the then part of the if expression as
return o :=: push_back_o
which lets the procedure body collapse to
return ((\push_back_o & (o :=: push_back_o)) | read_o())

and again to
return (o :=: \push_back_o) | read_o()

Alan D. Corre, author of the soon-to-be-published book, Icon Programming for Humanists, sent the following:
I wanted to write an Icon procedure to check if a string has precisely 22 characters (the size of the Hebrew alphabet) and no duplicate characters, so I wrote the following:
   procedure checkstring(abc)
   local cs, current
     if *abc ~= 22 then fail  # check length
     cs := ' '                # initialize cset
     abc ? every 1 to 22 do {
         current := move(1)   # select a char
         if cs ** current ~=== ' ' then fail
                              # already a member
         cs ++:= current }	   # char is ok
      return
   end
The procedure worked fine, but I said: "That isn't an Icon procedure. It's a thinly disguised Pascal function. Now write an Icon procedure." So I forsook "if mouse in hole" and wrote:
   procedure checkstring2(abc)
   local t, current
      if *abc ~= 22 then fail
      t := table()
      abc ? every 1 to 22 do {
         current := move(1)
         /t[current] | fail
         t[current] := 1 }
      return
   end
More Icon constructs, but no better really. Then I wrote:
   procedure checkstring3(abc)
      return *abc = *cset(abc) = 22
   end
Now that's an Icon procedure.


Icon home page