Smalltalk Programming Language Tutorial: Expressions, Messages, and Code Blocks

Smalltalk Programming Language Tutorial

Evaluating Expressions

This section explores various expressions in the Smalltalk environment and explains their results.

Unary Messages

  • # ( ‘under’ ‘of’ ‘strings’) size: Returns the number of items (3) in the array.
  • “Today is Thursday ‘asUpperCase’: Converts the string to uppercase (“TODAY IS THURSDAY”).
  • ‘hello Here I am’ reversed: Reverses the order of characters in the string (“yotse iuqa Aloha”).

Binary Messages

  • ‘hello’, ‘Here I am’: Concatenates the strings (“hello Here I am”).
  • # (1 2 3), # (4 5 6): Concatenates the arrays into a single array (1 2 3 4 5 6).
  • 4 = 5: Evaluates the equality of the numbers (false).

Keyword Messages

  • ‘This is a test’ at: 3: Returns the character at position 3 (“t”).
  • ‘Hello’ includes: $ o: Checks if the string includes the character ‘o’ (true).
  • ‘hello’ at: 1 put: $ H: Replaces the character at position 1 with ‘H’ (“Hello”).

Nested Messages

Nested messages involve evaluating expressions within expressions. Parentheses can be used to control the order of evaluation.

  • ‘Hello’ size + 4: Evaluates to 8 (size of ‘Hello’ is 5, plus 4).
  • ‘now’ size + # (1 2 3 4) size: Evaluates to 9 (size of ‘now’ is 3, size of the array is 4, and their sum is 7).

Global Variables

Global variables in Smalltalk start with a capital letter. Examples include:

  • Display: Represents the screen.
  • Transcript: A transcript window.

Comparing Objects

Smalltalk provides operators for comparing objects:

  • 3 < 4: true (3 is less than 4).
  • # (1 2 3 4) = # (1 2 3 4): true (the arrays have the same elements in the same order).
  • ‘hello’ <= ‘goodbye’: false (string comparison is lexicographical).

Code Blocks

Code blocks are anonymous functions that can take arguments and be passed around.

  • [$ a isVowel] value: Checks if the character ‘a’ is a vowel (true).
  • [3 + 4. hello asUpperCase] value: Evaluates to ‘HELLO’ (performs the sum and then converts ‘hello’ to uppercase).

Boolean Expressions

Boolean expressions combine conditions using and:, or:, and not.

  • 5 < 2 or: [$ a isVowel]: true (at least one condition is true).
  • 5 < 2 and: [$ a isVowel]: false (both conditions need to be true for and:).

Object Classes

The class message returns the class of an object.

  • # (Jackie Marisa Francesca Bree) class: Array
  • ‘Vijay Rakesh Charles Daniel Tyler’ class: String
  • 5 class: SmallInteger

Cascading Messages

Cascading allows sending multiple messages to the same object.

  • 3 factorial, factorial, factorial: Calculates the factorial of 3, then the factorial of the result, and so on.

Conditionals

Smalltalk uses ifTrue:ifFalse: for conditional execution.

  • 3 < 4 ifTrue: [block true ‘] ifFalse: [block false ‘]: Executes the “block true” code since 3 is less than 4.

Additional Examples

The document also includes examples of Smalltalk code for various tasks, such as calculating the quotient and product of two numbers, raising a number to a power, solving quadratic equations, checking for prime numbers, determining even or odd numbers, finding multiples of numbers in a list, identifying perfect numbers, and converting the case of characters in a string.