0704.315 Programming Languages

Homework 3


Due Date

Both Sections: Monday, November 5

Form of Submission

Homework can submitted via email or printout by deadline.

Preparation

This assignment will introduce you to the Squeak Smalltalk environment. Before you begin, do the following:
  1. Read Smalltalk in Brief, Chapter 2.

  2. Install Squeak on your machine.
    In the lab
    Open \\NEPTUNE\COMMON\OPENAREA\STUDENTS\Computer Science\Clamen\PL and copy the Squeak subdirection into your personal "H:" drive. Change the permission on the files so you have write access to them.
    On a computer at home running a Windows OS:
    Copy the "zip" archive listed on the class web page onto your machine, extract the files archived within into any directory.
    On a computer at home running another OS:
    Follow the other platforms link on the class web page and follow the directions as appropriate for your operating system

    1. Launch Squeak by double-clicking on squeak.exe.
    2. Minimize the various initial windows by clicking on the circle on the far right of their title bars.
    3. Open a new workspace by left-clicking the mouse in the root window, selecting Open... and then Workspace.
    4. You can save the contents of the Workspace at any time, by selecting the second icon from the left on the title bar, and select save contents to file....
    5. To quit Squeak (and save your work to date), bring up the World menu (left-click from the root window), and select save and quit.





Assignment

Your submission for this assigment should be a saved copy of your Workspace session, including the results of all the "evaluations" requested in the questions below.

  1. In the Workspace:
    1. Enter 3 + 10 into the Workspace. Select it with the mouse, left click and select print it. Record the answer.
    2. Enter and evaluate: 3 + 10 * 8
    3. Enter and evaluate: (3 + 10) * 8. Why is the answer different?
    4. Enter and evaluate: 3 squared + 9.
    5. Enter and evaluate: 3 + 9 squared and (3 + 9) squared. Explain the difference in answers.

  2. Enter the following expressions:
    x := 12. (x < 10) ifTrue: ['True'] ifFalse: ['False']
    Select print it to evaluate them. The second line demonstrates how conditional expressions are programmed in Smalltalk: as messages sent to Boolean objects.

  3. Let us turn the previous expression into a procedure-like object, called a block:
    test := [:x | (x < 10) ifTrue: ['True'] ifFalse: ['False']]
    1. Highlight with the mouse and select do it to introduce it into the Smalltalk interpreter. Then enter, highlight, and evaluate:
      test value: 12
      and then:
      test value: 2
      The block test acts similar to something we saw in Scheme: a procedure of one argument. In Scheme, we applied procedures by putting the procedure (or a variable bound to it) in the first position of a parenthetical expression and with the arguments in the other places. In Smalltalk, everything is an object and objects accept messages. To apply the procedure, we pass it the message value along with an argument.

    2. Blocks can have multiple arguments as well. Define and test the following:
      add := [:x :y | x + y ] add value: 2 value: 10
    3. Blocks can also have zero arguments. Define and test the following:
      sayHello := ['Hello!'] sayHello value

    4. Notice how the syntax for sayHello is similar to the arguments required for the ifTrue and ifFalse messages. Evaluate the following:
      (12 < 10) ifTrue: ['True'] ifFalse: sayHello

    5. The Smalltalk class Boolean has two subclasses: True and False. Boolean requires that its subclasses support (i.e., provide implementations for) the messages ifTrue and ifFalse, similar to pure virtual functions in C++. Sketch out implementations for ifTrue and ifFalse for the two subclasses.

  4. Conditionals are not the only construct to work by passing blocks (procedures) to objects. While and "for" loops work similarly:
    1. Enter and evaluate the following sequence of Smalltalk expressions:
      x := 0 . 1 to: 5 do: [:i | x := x + i] . x
    2. And this sequence:
      remainder := [:x :y | [x < y] whileFalse: [ x := x - y ] . x ] . remainder 12 5