Programming Corner from Icon Newsletter 5


December 31, 1980; Icon Version 2


This programming corner comes in the form of puzzles and posed questions, with solutions and answers to appear in the next Newsletter.

1. What is the output produced by each of the following expressions?
every write((0 | 0) to 7)
every write(0 to 3,0 to 7,0 to 7)
every write(1 | 2 to 3 | 4 by 1 | 2)
every 1 to 3 do every write(1 to 3)
2. For arbitrary procedures f(x,y) and g(x,y), what is the sequence of calls produced by
every (f | g)(1 to 3, 4 | 5)
3. Given
s1 := "aeiou"
s2 := "abecaeioud"
what are the outcomes of
(find | upto)(s1,s2)
(find | upto)(s2,s1)
(if size(s1) > size(s2) then upto else find)(s1,s2)
4. What are the outcomes of the following expressions? (Note any that produce errors.)
(x | y) := 3
(x & y) := 3
(1 & x) := 3
(x & 1) := 3
(x + 1) := 3
5. Given the procedure
procedure drive(x)
   fail
end
What is the output produced by
drive(write(1 to 7))
drive(write(0 to 7,0 to 7))
6. What does execution of the following program do?
procedure main()
   f(f := write)
end
7. The following procedure is proposed as a generator of "words" -- strings of consecutive letters -- in the lines of the input file. It does not work properly, however. What does it actually do and what is the cause of the problem? Rewrite the procedure to work properly.
procedure genword()
   local line
   static letters

   initial letters := &lcase ++ &ucase

   while line := read() do
      line ?
         while tab(upto(letters)) do
            suspend tab(many(letters))

end

Icon home page