# Smalltalk

- Appeared in:
- 1970s
- Influenced by:
- Influenced:
- Paradigm:
- Typing discipline:
- File extensions:
- .st, .cs, .image, .changes
- Versions and implementations (Collapse all | Expand all):

Smalltalk is a pure object-oriented programming environment. This environment includes the source code, editor, compiler development tools and debugger all in one binary “image” file which is loaded into memory and interpreted.

Smalltalk has a relatively minimal syntax and a clean object-oriented model with no primitive types or other oddities commonly found in other object-oriented languages.

## Elements of syntax:

Inline comments | "This is a comment" |
---|---|

Case-sensitivity | yes |

Variable assignment | var := value. |

Variable declaration | | variableName | |

Variable declaration with assignment | | var | var := value. |

Block | [ ... ]. |

Physical (shallow) equality | == |

Physical (shallow) inequality | ~~ |

Deep equality | = |

Deep inequality | ~= |

Comparison | < > <= >= |

Function call | obj f:a g:b ... (method) or f value:a value:b ... (function object) |

Function call with no parameters | obj f (method) or f value (function object) |

If - then | value ifTrue: [ code ]. |

If - then - else | value ifTrue: [ code ] ifFalse: [ more code ]. |

Loop forever | [ code ] repeat. |

While condition do | [ condition ] whileTrue: [ code ]. |

For each value in a numeric range, 1 increment | (1 to: 10) collect: [ :each | each + 1 ] |

For each value in a numeric range, 1 decrement | (1 to: 10) collect: [ :each | each - 1 ] |

## Links:

## Examples:

### Hello, World!:

Example for versions Squeak 3.10This example would pop up an information box with “Hello, World!’ as text, and an “Okay” button.

```
self inform: 'Hello, World!'.
```

### Fibonacci numbers:

Example for versions Squeak 3.10This code is a method of an object, and adds n Fibonacci numbers to an OrderedCollection called `numbers`

.

Variable declarations are enclosed in vertical bars and don’t specify their type. Code blocks are enclosed in square brackets. The “^” carrot at the end returns the result from this method.

```
fibonacci: n
| numbers a b |
numbers := OrderedCollection new.
a := 1.
b := 1.
[ numbers size < n ] whileTrue: [
| temp |
numbers add: a.
temp := b.
b := a + b.
a := temp.
].
^ numbers.
```

### Fibonacci numbers:

Example for versions Squeak 3.10This is a recursive method implementing the Fibonacci sequence, writing it to the given stream of numbers.

To invoke this method:

stream := WriteStream on: (Array new: 100). Fibonacci new fibonacci: 1 and: 1 writeTo: stream

There is no halting condition; the program will continue writing Fibonacci numbers until interrupted.

This example is far from optimal: the stream will automatically double the size of the array each time it is filled (a very expensive operation in Squeak), and the recursion continually consumes memory. The alternative iterative example is much more efficient.

```
fibonacci: n1 and: n2 writeTo: stream
stream nextPut: n1.
self fibonacci: n2 and: (n1+n2) writeTo: stream.
```

### Factorial:

Example for versions Squeak 3.10This example is actually cut and paste from the Squeak core source code in the Integer class. From any Squeak image:

10000 factorial. “and press alt-p to print the result below this line”

284625968091705451890641321211986889014805140170279923079417999427441134000376444377299078675778477581588406214231752883004233994015351873905242116138271617481982419982759241828925978789812425312059465996259867065601615720360323979263287367170557419759620994797203461536981…etc… edited for brevity.

This takes about 200 milliseconds on a modern PC to compute the factorial, and about 6 seconds to convert the resulting number into a String for displaying.

The resulting number has this 35660 digits:

10000 factorial asString size

35660

```
factorial
"Answer the factorial of the receiver."
self = 0 ifTrue: [^ 1].
self > 0 ifTrue: [^ self * (self - 1) factorial].
self error: 'Not valid for negative integers'
```

### Hello, World!:

Example for versions gst 3.1```
'Hello, World!' printNl.
```

### Fibonacci numbers:

Example for versions gst 3.1This example uses iterative definition of Fibonacci numbers,.

```
a1 := 0.
a2 := 1.
0 to: 15 do: [ :i |
a2 display.
t := a1 + a2.
a1 := a2.
a2 := t.
', ' display
]
'...' displayNl.
```

### Factorial:

Example for versions gst 3.1Smalltalk provides built-in factorial method of class Number, so the example is really simple.

```
0 to: 16 do: [ :i |
i display.
'! = ' display.
i factorial displayNl
].
```

## Comments

]]>blog comments powered by Disqus

]]>