Search code examples
matrixblocksmalltalklazy-sequencess-expression

Lazy list s-expression matrix in smalltalk


So I have a class to create in smalltalk called LazyMatrix. The class only has 1 instance variable and cannot be a subclass of anything but Object. The instance variable of LazyMatrix is called block and must be a back. I initialize LazyMatrix like this:

initialize
block:=[nil]

There will be a method for setting values

setRow:column:value:

This method will redefine the block by setting the new block as [#(i j value).[nil]]. Each subsequent call adds an array of 3 to the block, so it expands like [#(i j value).[#(i j value).[nil]]] much like an s-expression or "lazy list".

So I need to access the head of this block (i.e. [#(i j value) ) as well as the tail of this bock (i.e. [#(i j value).[nil]] ). How do I do this in smalltalk? I know calling value on the block will return the tail... now I need to return the head.


Solution

  • Adding an array to your block doesn't get you anywhere. Think in terms of behavior, not about data structures.

    I guess your teacher did provide you with the overall idea: given a row and column, answer the associated value; otherwise ask the "tail" for the answer.

    That is literally how you would implement it in Smalltalk using a block closure (where the "tail" is the previously set block). Inside the block, put code that does the testing and answering and tail recursion. Do not make a data structure, tail and head are just metaphors to reason about this style of coding.

    I just implemented your LazyMatrix in Squeak, it is just a few lines of code. Cute example, indeed. And no arrays or collections involved, at all.

    Hint: The key to this puzzle is realizing that every call to setRow:column:value: can create a new block, which is independent of all the previously created blocks.