really silly question

Randal L. Schwartz merlyn at stonehenge.com
Mon Sep 3 15:21:08 UTC 2001


>>>>> "Lex" == Lex Spoon <lex at cc.gatech.edu> writes:

Lex> Overall, I'm really starting to think loops should be treated like 
Lex> assembly language or two's complement: interesting for the
Lex> terminally curious, probably useful to know a *little* about, but not
Lex> incredibly useful in most programs.  (Of course, this is only true in a
Lex> nice language that actually has collections and blocks!)

We find this true in Perl.  Perl has some nice "iterate over this
list" constructs, like foreach, map, and grep, which act like
do:, collect:, and select:.  (Yes, we're still missing inject:into:, but
that's coming in the next release of Perl as reduce.)  And Perl also
has a very C-like "for" construct, permitting the user to select
a starting condition, termination condition, and stepping condition.

Almost invariably, the loops I construct with "for" require about five
times as much thinking to avoid fencepost problems or confusing setup
or termination conditions.  When I cast the problem in terms of
foreach/map/grep/reduce, I never worry about that.  I just say "do
this for that", and it just works.

And I've noticed the same things in the students I teach... if they
start out with a C or Java background, they start with the familiar
"for" loop (which we deliberately teach later rather than earlier),
but then *progress* to "speaking native Perl", and find the same thing
I've found: using higher-level constructs removes debugging time, and
reduces time-to-market delays and maintenance costs.

Oddly, I often get a student, upon seeing the foreach contruct, ask
"but how do I know which element of the list it's accessing at the
moment?".  I always grin and say, "you don't, and that's the beauty".
I feel like a Zen master at that point. :-)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!




More information about the Squeak-dev mailing list