Programming Corner from Icon Newsletter 36


July 1, 1991; Icon Version 8

Lots of interesting things come across our "electronic desk". Here's an exchange that dates back to 1987 that we've just unearthed. It's a bit on the bizarre side, but you may find it worth a chuckle.

Steve Wampler:
I suspect you've seen this kind of thing before, but it's the first time a student has come up with it. After talking about lists, stacks, and queues, I gave as part of an assignment the problem of writing a file out with the lines displayed last-to-first. One student wrote:
procedure main()
   if line := read() then {
      main()
      write(line)
      }
end
Oh well.

Ken Walker:
This can of course be written more compactly:
procedure main()
   write(read(), main())
   return
end
By the way, didn't something like this come up before?

Dave Gudeman:
How about a more Icon-like solution:
procedure main()
   write(read(), main() | "")
end
Steve Wampler:
Compare these two programs:
procedure main()
   (write(read(), main()))
   return
end

procedure main()
   (write(read()), main())
   return
end
(The returns aren't really needed, but one of them would have to be modified slightly to work properly -- destroying the similarity.)

On a saner note, Steve Wampler also contributes the following procedure:
# sleep (restlessly) for n seconds

procedure sleep(n)
   local start

   start := &time
   while &time < start + n * 1000

   return
end
He comments "The nice thing about it is that it can 'sleep' for fractions of a second -- sleep(0.5) sleeps for half a second". Note that this depends on adequate clock resolution.

Icon home page