-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
=pod
It's always fun to watch someone discover Lisp macros:
L
Clearly they're the solution to every problem, and there's no way to
solve the problems they solve in languages without them!
I used to think that, anyway, but as it turns out that's usually not
the case. In fact, I've written and thrown away quite a few articles
that clearly showed that Perl needs macros. I usually prove myself
wrong by the end.
Anyway, in that above article, the author writes a macro that is like
C, but takes multiple variables. Translating to Perl, we
want to write this:
lang:Perl
our ($x, $y, $z);
do_many_times {
say "$x * $y * $z: ", $x * $y * $z;
}
x => 5,
y => 4,
z => 3;
Instead of the longer:
for my $x (1..5){
for my $y (1..4){
for my $z (1..3){
say "$x * $y * $z: ", $x * $y * $z;
}
}
}
Here's the implementation of C:
sub do_many_times(&@){
my ($code, @defs) = @_;
return $code->() if !@defs;
my $var = shift @defs;
my $top = shift @defs;
no strict 'refs';
$$var = 0;
for my $count (1..$top){
local $$var = $count;
&do_many_times($code, @defs);
}
}
The way it works is by taking each variable / upper bound pair, and
making a loop that calls C with the remaining
variables. In the loop we create the C<$whatever> variable from
C. Finally, if there are no variable definitions passed to
the function, then we just execute the code block.
No macros, just functional programming and a symbolic reference :)
Don't get me wrong, Lisp is one of my favorite programming languages.
But you can definitely get by without macros in other languages.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFIAtMq2rw+dVvzZm0RAgKJAJ4iMO9FnFi5P+dufKcXagy40uWKYQCfUejp
BgbUvfx9e0MLL9ktqNGaR5k=
=izdO
-----END PGP SIGNATURE-----