[Newbies] Teaching Smalltalk
Hannes Hirzel
hannes.hirzel at gmail.com
Tue May 4 15:26:08 UTC 2010
On 5/4/10, Bert Freudenberg <bert at freudenbergs.de> wrote:
> On 03.05.2010, at 20:30, David Mitchell wrote:
>> Bummed that Why Smalltalk is gone.
>
> Yeah. It's still in the archive though:
>
> http://web.archive.org/web/*/whysmalltalk.com
>
>> Here is my crack at translating. Wouldn't say this is idiomatic Smalltalk
>> (transliterated C never will be). But it ran in the transcript.
>>
>> Tried to only use ANSI Smalltalk:
>>
>> | count doors |
>> count := 10.
>> "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]
>>
>
> This looks a lot like
> http://programming.dojo.net.nz/languages/smalltalk/index
> (although that code doesn't even run - could someone fix?)
>
> How about this one - demonstrates better what Squeak provides:
>
> | doors |
> doors := Array new: 100 withAll: false.
> 1 to: doors size do: [:pass |
> doors := doors collectWithIndex: [ :isOpen :door |
> (door isDivisibleBy: pass) xor: isOpen]].
> ^ (1 to: doors size) select: [:door | doors at: door]
>
>
> - Bert -
>
>
>
I would include both examples. The first one using C-like constructs
thus giving some idea how the syntax constructs relate, the other one
as an example of idiomatic Smalltalk which allows for compact code.
--Hannes
More information about the Beginners
mailing list