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.
STEP 1
Reverse a word
reversed returns a new string with its characters in reverse order.
'Squeak' reversed
The answer is kaeuqS.
STEP 2
Keep the original
A message can return a new object. reversed does not edit the original string.
| word |
word := 'Squeak'.
word reversed.
word
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.
#('red' 'green' 'blue') reversed
Blue is first; each color name stays readable.
STEP 4
Reverse twice
Applying reversed twice restores the original order. Equality compares the resulting contents.
'Squeak' reversed reversed = 'Squeak'
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.
'level' reversed = 'level'
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.