Squeak 3.10

Version of implementation Squeak of programming language Smalltalk

Squeak 3.10 is the latest stable version of Squeak.


Hello, World! - Smalltalk (79):

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

self inform: 'Hello, World!'.

Fibonacci numbers - Smalltalk (93):

This 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 - Smalltalk (94):

This 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 - Smalltalk (95):

This 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


	"Answer the factorial of the receiver."

	self = 0 ifTrue: [^ 1].
	self > 0 ifTrue: [^ self * (self - 1) factorial].
	self error: 'Not valid for negative integers'