Cool. I've just understood the 100 doors problem looking at your code. I was too lazy to try to understand the original one on <meta http-equiv="content-type" content="text/html; charset=utf-8"><a href="http://programming.dojo.net.nz/languages/smalltalk/index">http://programming.dojo.net.nz/languages/smalltalk/index</a> :)<div>
<br clear="all">Laurent Laffont<br>
<br><br><div class="gmail_quote">On Tue, May 4, 2010 at 5:55 PM, John McKeon <span dir="ltr"><<a href="mailto:p3anoman@gmail.com">p3anoman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Looking at <a href="http://rosettacode.org/wiki/100_doors" target="_blank">rosettacode</a> most languages look nearly identical
(or, at the very least, ugly/crappy) when written out procedurely so
that one might end up deciding to just stick with C.<br><br>I know I am
going off the deep end a bit, but the whole answer to "why Smalltalk?"
lies in the fact that problems should/would be approached in a
completely different manner. As I am sure they would in most of the
other languages if one were not restricted to 20 or so lines of code.<br>
<br>Since it is example code, at least in the case of Smalltalk, an
object oriented solution would be in order - i.e. show how it would be
solved using "everything is an object". Unfortunately, it doesn't really
fit into "a few lines of code" to display on a page (even though the
classes/objects and code required to implement them is little more than a
few lines). And you also don't get any feel for the great tools.
Perhaps, some insight into how one would run the code in Java/Eclipse,
or Visual C <chuckle> with all the includes and project setup, etc
might be a useful addition to the comparisons...<br>
<br>Object subclass: #Corridor<br> instanceVariableNames: 'doors
count'<br> classVariableNames: ''<br> poolDictionaries: ''<br>
category: '100Doors'<br><br>initialize: anInteger<br>
"initialize the receiver with the given number of doors"<br>
count := anInteger.<br> doors := OrderedCollection new.<br>
anInteger timesRepeat: [ doors add: Door new ]<br><br>pass<br>
"iterate over the doors"<br>
1 to: count do: [ :i | self passBy: i ]<br><br>passBy: anInteger<br>
"if the nth door is open close it otherwise open it"<br> doors by:
anInteger do: [ :door | door toggle ]<br><br>printOn: aStream<br>
"print the open doors"<br> aStream cr.<br> doors withIndexDo:
[ :door :i | door isOpen ifTrue: [ aStream nextPutAll: i asString, ' is
open'; cr ]]<br><br>Corridor class<br><br>pass: anInteger<br>
"return a new Corridor with the given number of doors that has been
passed thru"<br> ^self new <br> initialize: anInteger; <br>
pass<br><br><br>==================================================================<br>
<br>Object subclass: #Door<br> instanceVariableNames: 'isOpen'<br>
classVariableNames: ''<br> poolDictionaries: ''<br> category:
'100Doors'<br><br>isOpen<br> "Answer the value of isOpen"<br>
^ isOpen<br><br>toggle<br> "if the receiver is open close it else
open it"<br> isOpen := isOpen not<br><br>initialize<br>
"initialize the receiver to be closed"<br> super initialize.<br>
isOpen := false<br><br>===========================================================<br>Patch
to iterate over a collection by each nth item<br>
<br>OrderedCollection>>by: anInteger do: aBlock<br> | index |<br>
index := anInteger.<br> [index <= lastIndex]<br>
whileTrue: <br> [aBlock value: (array at: index).<br>
index := index + anInteger]<br><br><br>Probably silly for the
problem given but just my 2 cents<br>John<br><font color="#888888"><br clear="all"><br>-- <br><a href="http://john-mckeon.us/seaside" target="_blank">http://john-mckeon.us/seaside</a><br>
</font><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a><br>
<a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners" target="_blank">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>