I am using Smalltalk to type in Transcript window a 1-9 multiplication table
. Here is my code: *1 to: 9 do: [:i| 1 to: i do: [:j| Transcript show: j. Transcript show: ' * '. Transcript show: i. Transcript show: ' = '. Transcript show: j * i. Transcript show: ' '. ]. Transcript show: ' '; cr. ].* As can be seen, the above code, although working fine, looks far from beautiful and concise. I had hoped to write something like : *Transcript show: j '*' i '=' j * i.* Unfortunately, they are wrong. I remember C has a very nice way to handle that issue of mine. Like, *printf("%d * %d = %d ", j, i, j * i);* Is there a more elegant way to make Smalltalk codes elegant in this situation ? ----- Dig, dig where you are, Down below's well. Let those that walk in darkness shout, Down below's hell. -- Sent from: http://forum.world.st/Squeak-Beginners-f107673.html _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners
Dig, dig where you are,
Down below's well. Let those that walk in darkness shout, Down below's hell. |
Hmm... 1 to: 9 do: [:i | 1 to: i do: [:j | Transcript showln: ('{1} * {2} = {3}' format: {i.j.i*j}) ]]. Best, Marcel
_______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Ooops, I messed with the layout. Here again: 1 to: 9 do: [:i | 1 to: i do: [:j | Transcript show: ('{1} * {2} = {3} ' format: {i.j.i*j}) ]. Transcript cr]. So, you can use #format: to format strings more concisely. Best, Marcel
_______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
/Thanks. That is really concise. Its formatting style is very much like that
of C printf. Just wondering who copied who?/ ----- Dig, dig where you are, Down below's well. Let those that walk in darkness shout, Down below's hell. -- Sent from: http://forum.world.st/Squeak-Beginners-f107673.html _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners
Dig, dig where you are,
Down below's well. Let those that walk in darkness shout, Down below's hell. |
Free forum by Nabble | Edit this page |