[Newbies] Teaching Smalltalk
Bert Freudenberg
bert at freudenbergs.de
Tue May 4 15:45:39 UTC 2010
On 04.05.2010, at 08:26, Hannes Hirzel wrote:
>
> 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.
Though (at least in this case) the "C-like" version is equally compact if formatted similarly:
| doors |
doors := Array new: 100 withAll: false.
1 to: doors size do: [:pass |
pass to: doors size by: pass do: [ :door |
doors at: door put: (doors at: door) not ] ].
^ (1 to: doors size) select: [:door | doors at: door]
I have to admit I don't like most examples involving Transcript. They are usually written by people who expect output to work like a teletype console from the 1960s. This completely misses the point about Smalltalk as an interactive development environment.
- Bert -
More information about the Beginners
mailing list