1.3 Writing your first scripts

In this section you will learn how to write simple scripts in the Workspace to get a taste and feel for Smalltalk code. The examples are associated with small exercises to experiment with and accompanied with solutions in the appendix. We intentionally kept the details of the syntax out of this section.

In a Workspace, the usual Hello World! program can be written in Smalltalk:

Transcript show: 'Hello World!'

Example 1.1: The traditional ’Hello World!’ program

To execute this code, select it with the mouse and over it do ...right mouse click → Do it (d)... You may now see nothing happen! Indeed to see the output, you need a Transcript window to be visible. The Transcript is a place where a programmer can send information to the user as we are doing here. Do ...World menu → Open...Transcript... and execute the code again.

ch01-img2

Figure 1.3: Transcript window with output

The workspace code has three parts:

The action of printing takes place in the class Transcript. The code execution is also invoked with keyboard shortcuts Ctrl-a (select All) then Ctrl-d (Do it).

Transcript show: 'Hello World!'.
Transcript newLine.
Transcript show: 'I am Cuising'

Example 1.2: Multiple lines

In this three line script, observe how the lines are separated by a dot “.”. This dot (or period) is a line separator so it is not needed in the third line nor in a one line script. The message #newLine has no argument.

A String is the way text is represented in a programming language, it is a collection of characters. We already met string with our first script, it is enclosed in single quotes: 'hello world!'. We capitalize it with the #capitalized message:

Transcript show: 'hello world!' capitalized
⇒ 'Hello world!' 

To convert all the characters to capitals use the #asUppercase message:

Transcript show: 'hello world!' asUppercase
⇒ 'HELLO WORLD!'

Two strings are concatenated with the #, message:

Transcript show: 'Hello ', 'my beloved ', 'friend'
⇒ 'Hello my beloved friend'

Example 1.3: Concatenate strings

 CuisLogo Add a message to modify Example 1.3 to output ’Hello MY BELOVED friends’.

Exercise 1.2: Concatenate and uppercase

1.3.1 Fun with numbers

In your Workspace, to compute a factorial execute the example below with Ctrl-a then Ctrl-p (Print it):

100 factorial
⇒  9332621544394415268169923885626670049071596826438162146859
29638952175999932299156089414639761565182862536979208272237582
51185210916864000000000000000000000000

Cuis-Smalltalk handles very large integer numbers without requiring a special type declaration or method. To convince yourself try the example below:

10000 factorial / 9999 factorial
⇒ 10000

If you execute and print with Ctrl-p the code: 10000 factorial, you will see that it takes far more time to print one factorial result than to compute two factorials and divide them. The result is an integer as expected, not a scaled decimal number as many computer languages will return.

As we are discussing division, you may not get the result you expect:

15 / 4
⇒ 15/4

It looks like Cuis-Smalltalk is lazy because it does not answer the decimal number 3.75 as we were expecting. In fact Cuis-Smalltalk wants to be as accurate as possible, and its answer is a rational number! After all, a fraction is just a division we are too lazy – because it is troublesome – to compute, Cuis-Smalltalk does just that!

Try out this to understand what is happening underneath:

(15 / 4) + (1 / 4)
⇒ 4

Is it not wonderful? Cuis-Smalltalk computes with rational numbers. We started with division and addition operations on integer, and we got an accurate result thanks to intermediate computation on rational numbers.

 note In the example, observe how the parentheses are used although in arithmetic calculation the division is performed first. With Cuis-Smalltalk you need to specify the order of operations with parentheses. We will explain why later.

 CuisLogo Write the code to compute the sum of the first four integer inverses. 4 inverted is 1/4

Exercise 1.3: Inverse sum

Integers can be printed in different forms with the appropriate message:

2020 printStringRoman ⇒ 'MMXX' 
2020 printStringWords  ⇒ 'two thousand, twenty'
"Number as the Maya did"
2020 printStringBase: 20 ⇒ '510'

 CuisLogo Print 2020 as words, capitalized.

Exercise 1.4: Capitalize number as words

The integer and float numbers we have played with are Numeric Literals. Literals are building blocks of the language and considered as constant expressions. They literally are as they appear.

There are several syntax variants which denote a number:

Numeric literalWhat it represents
1integer (decimal notation)
2r101integer (binary radix)
16rFFinteger (hexadecimal radix)
1.5floating point number
2.4e7floating point (exponential notation)

Depending on the value we need to use, we can mix these literal representations:

16rA + 1 + 5e-1 + 6e-2
⇒ 289/25