I'm just trying to do an assignment in Squeak (Smalltalk), and I can't figure out how to create a matrix with a specific number of rows and columns.
I know that one can create a 3x3 matrix with something like A := Matrix new:3.
, but I need something like a 3x2 matrix. The System Browser isn't much help to my understanding of how to do this, and I've been searching the internet for awhile and haven't found much of anything to help. Could anyone give an example line of code of how to create something like a 3x2 matrix?
What you should understand is how Smalltalk deals with constructors - always check the Class class.
If you check the Matrix class
you will find out that the #new:
internally uses ^ self rows: dimension columns: dimension
. That means your:
aMatrix := Matrix new:3
actually does
aMatrix := Matrix rows: 3 columns: 3
, which happens to happen to be what you are searching for.
In my eyes, the #new:
message is not a good choice. I would prefer to have a #squareSize:
or something like it.
Note: Don't use capitals for variables (comment based on your A :=
assigment)
Edit: Why not to use capitals for local/instance variables?
The capital letter at the beginning is reserved for global variables. You have such like Transcript
, Smalltalk
, etc. For example, class names are also globals that is why you have Matrix
and not matrix
. For class instance you use prefix with a
or an
like I have used aMatrix
is an instance of class Matrix
.
There can be suttle differences among different Smalltalk flavours. For example, in Smalltalk/X the class variable starts with a capital letter, which is, as far I could see, not the case in GNU Smalltalk.