CSE 505 -- Smalltalk Mini-Exercises

As usual, these are just for practice, and not to hand in.
  1. Write a Smalltalk expression to return true if x is either equal to 0 or if 10 divided by x equals y.
  2. Let a be an array of some sort. Write a loop that prints every second element on an output stream strm. To get you started, here is some code that initializes a to an array with some objects, and that prints the first element on strm, and finally returns the contents of the stream.
      | a strm |
      strm := WriteStream with: (String new: 100).
      a := Array new: 10.
      a at: 1 put: 'fee fi fo fum'.
      a at: 2 put: 3.14159.
      a at: 3 put: 8.
      (a at: 2) printOn: strm.
      strm contents
    

  3. Add a repeatUntil control structure to Smalltalk. Hint: this is trivial -- do it by defining a repeatUntil: method for BlockContext.
  4. Consider the following Smalltalk class definition.
    Object subclass: #Octopus
       instanceVariableNames: 'myblock'
       classVariableNames: ''
       poolDictionaries: ''
    
    setValue: x
       myblock := [x].
    
    setBlock: y
       myblock := y.
    
    getValue
      ^ myblock value
    
    What is the result of evaluating the following code? (The value in each case will be the value of the last o getValue expression.)
          | o a |
       a := 3.
       o := Octopus new.
       o setValue: a.
       a := a+1.
       o getValue
    
    And what about this code?
          | o a b |
       a := 3.
       b := [a].
       o := Octopus new.
       o setBlock: b.
       a := a+1.
       o getValue