# [Newbies] Teaching Smalltalk

John Foster johnps11 at bigpond.com
Tue May 4 04:45:03 UTC 2010

```And here we see how someone who doesn't grok smalltalk sees the world.  A
closure could work like a function though.

What I'm trying to work out is how to get rid of all the to:by:do: code -
I feel as if it should be possible to use inject:into: to get to the
answer much faster.

John

doorProblem := [ :count |
a  := Array new: count withAll: false.

a withIndexDo: [ :each :index |
index to: (a size) by: index do: [:this |
a at: this put: (a at: this) not
]
].

"output"
a withIndexDo: [ :each :index |
each ifTrue: [Transcript show: ('Door ', index asString, ' is
open') ; cr]
].
Transcript show: 'All the rest are closed';cr.
].

doorProblem value: 100.

> Dear David,
>
> Thanks for this.
>
> I'm happy to use idiomatic Scheme concepts.
>
> Are you able to make this a function which can work for n doors?
> 	Example: http://programming.dojo.net.nz/languages/cpp/index
>
> Kind regards,
> Samuel
>
> On 4/05/2010, at 3:31 PM, David Mitchell wrote:
>
>> Doh! Was only playing with 10 doors. This makes it 100.
>>
>> | count doors |
>> count := 100.
>> "Initialize the array of doors to 0 (closed)"
>> doors := (Array new: count) atAllPut:  false.
>>
>> "Process the doors"
>> 1 to: count do:
>>     [ :pass |
>>     pass to: count by: pass do:
>>         [ :door |
>>         doors
>>             at: door
>>             put: (doors at: door) not ] ].
>>
>> "Print out the results"
>> 1 to: count do:
>>     [ :n |
>>     Transcript
>>         show: 'door #', n, ' is ';
>>         show: ((doors at: door) ifTrue: [#open] ifFalse: [#closed]);
>>         cr]
>> _______________________________________________
>> Beginners mailing list
>> Beginners at lists.squeakfoundation.org
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>

```

More information about the Beginners mailing list