s LearnSqueak.com

ABSOLUTE BEGINNER

Your first reversed: turn an order around

Reverse a word and a list, keep the original, and spot a palindrome in five tiny visual Smalltalk steps.

5 minutes · 5 small steps · Squeak 6.1

STEP 1

Reverse a word

reversed returns a new string with its characters in reverse order.

Select the whole expression, then Print it
'Squeak' reversed
NATIVE SQUEAK CAPTURE
Native Squeak reversed checkpoint 1
Actual native Squeak Workspace Print it result.
You should see

The answer is kaeuqS.

STEP 2

Keep the original

A message can return a new object. reversed does not edit the original string.

Select the whole expression, then Print it
| word |
word := 'Squeak'.
word reversed.
word
NATIVE SQUEAK CAPTURE
Native Squeak reversed checkpoint 2
Actual native Squeak Workspace Print it result.
You should see

The variable still refers to Squeak.

STEP 3

Reverse a list

The same message also works on an Array. It reverses whole elements, not the letters inside each element.

Select the whole expression, then Print it
#('red' 'green' 'blue') reversed
NATIVE SQUEAK CAPTURE
Native Squeak reversed checkpoint 3
Actual native Squeak Workspace Print it result.
You should see

Blue is first; each color name stays readable.

STEP 4

Reverse twice

Applying reversed twice restores the original order. Equality compares the resulting contents.

Select the whole expression, then Print it
'Squeak' reversed reversed = 'Squeak'
NATIVE SQUEAK CAPTURE
Native Squeak reversed checkpoint 4
Actual native Squeak Workspace Print it result.
You should see

Try another word and check that the answer stays true.

STEP 5

Spot a palindrome

A palindrome reads the same in both directions. This comparison is exact: spaces and capital letters count.

Select the whole expression, then Print it
'level' reversed = 'level'
NATIVE SQUEAK CAPTURE
Native Squeak reversed checkpoint 5
Actual native Squeak Workspace Print it result.
You should see

Try 'Level': the capital L makes this exact comparison false.

YOU DID IT

A small step. A real result.

reversed returns elements in the opposite order without changing the original. Reversing twice restores the order. A palindrome equals its own reverse.

Optional code download: open it as text and copy one example into Workspace.

Download the five examples; copy one at a time

Your first list · More Smalltalk messages