Fibonacci numbers in Smalltalk

Example for versions Squeak 3.10

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.