a newbie question (disregard earlier message)
Richard A. O'Keefe
ok at atlas.otago.ac.nz
Thu Jan 10 03:36:25 UTC 2002
Hari Balaraman <hbalaram2 at yahoo.fr> wrote:
what follows is the method I wrote for the following problem:
write an array in which each element is the square of the
corresponding element in the receiver array.
I cannot find out what I am doing wrong:
Which class did you put this in?
I shall reindent the code for clarity.
squarearray := Array new: (self size).
"These parentheses .....^ ^ are not needed."
1 to self size do:
"^^ you are missing a colon here, it should be to: "
squarearray at: index put: ((self at: index)*(self at: index))].
"These parentheses are not ^ needed ^
Most array calculations are simplified if you hide the indices.
(The APL lesson.) This would be simpler as
^self collect: [:x| x*x]
assuming that the method is placed in Array, and in fact Squeak already
has almost exactly this in Collection>>squared, so
#(3 1 4 1) squared ====> #(9 1 16 1)
without you writing one line of code.
Of course in Squeak, you can just write
^ self * self
A somewhat more useful method would be
"in class Collection category arithmetic"
^self collect: [:x | x raisedTo: aPower]
Given all the other arithmetic methods, I wonder why this one was omitted.
More information about the Squeak-dev