Is there arithmetic sequence represents?

D

Davy

Hi all,

I found in Perl (2..8) is equal to (2,3,4,5,6,7,8).

But if I want a arithmetic sequence like (2,4,6,8,10,12), is there any
easy represents?

PS, in Matlab, is [2:2:12].

Thanks!
Davy
 
J

Jürgen Exner

Davy said:
I found in Perl (2..8) is equal to (2,3,4,5,6,7,8).

But if I want a arithmetic sequence like (2,4,6,8,10,12), is there any
easy represents?

for my $i (1..6) {
$i *= 2;
.....
}
 
U

Uri Guttman

D> Tony Curtis said:
On 9 Aug 2006 18:21:14 -0700,
"Davy" <[email protected]> said:
Hi all, I found in Perl (2..8) is equal to
(2,3,4,5,6,7,8).
But if I want a arithmetic sequence like
(2,4,6,8,10,12), is there any easy represents?
PS, in Matlab, is [2:2:12].

You could find a solution with a map...

map {$_ * 2} (1 .. 6);
D> [snip]
D> Hi,

D> But it seems not intuitive like [2:2:12]. Anyhow, thanks!

so use matlab or perl6 which has such a beast. perl5 doesn't have the
syntax for it.

uri
 
T

Ted Zlatanov

You could find a solution with a map...

map {$_ * 2} (1 .. 6);
[snip]

But it seems not intuitive like [2:2:12]. Anyhow, thanks!

Write your own function:

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

sub matlab_list
{
my $start = shift @_;
my $increment = shift @_;
my $end = shift @_;

my @ret;

while ($start <= $end)
{
push @ret, $start;
$start += $increment;
}

return @ret;
}

print "$_\n" foreach matlab_list(2, 2, 12);

prints:

2
4
6
8
10
12
 
A

anno4000

Ted Zlatanov said:
You could find a solution with a map...

map {$_ * 2} (1 .. 6);
[snip]

But it seems not intuitive like [2:2:12]. Anyhow, thanks!

Write your own function:

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

sub matlab_list
{
my $start = shift @_;

"shift @_" is the same as just "shift", which is much more common in
this function.
my $increment = shift @_;
my $end = shift @_;

If you have many arguments to pass (three is many), most people
would write that:

my ( $start, $increment, $end) = @_;
my @ret;

while ($start <= $end)
{
push @ret, $start;
$start += $increment;
}

return @ret;
}

print "$_\n" foreach matlab_list(2, 2, 12);

There's nothing wrong with your function, but it's awfully roundabout.
Apart from the argument-passing, you failed to follow Tony Curtis'
advice (still preserved near the beginning) to use map for list
generation. Your loop can be replaced with a little arithmetic
and a map:

sub matlab_list {
my ( $start, $inc, $end) = @_;
map $start + $inc*$_, 0 .. ($end - $start)/$inc;
}

Anno
 
T

Ted Zlatanov

"shift @_" is the same as just "shift", which is much more common in
this function.

I know this. I prefer to be explicit. I also say "shift @ARGV" in
the main body, so it's clear what I'm shifting.
If you have many arguments to pass (three is many), most people
would write that:

my ( $start, $increment, $end) = @_;

Great. My style is to do one per line, so I can comment each one
separately. I am aware of the (very minor) penalty, I think clear
comments are worth the sacrifice.
There's nothing wrong with your function, but it's awfully roundabout.
Apart from the argument-passing, you failed to follow Tony Curtis'
advice (still preserved near the beginning) to use map for list
generation. Your loop can be replaced with a little arithmetic
and a map:

sub matlab_list {
my ( $start, $inc, $end) = @_;
map $start + $inc*$_, 0 .. ($end - $start)/$inc;
}

I think my function is more suitable for a beginner example. The
function you show is absolutely terrible for that purpose IMHO. In
terms of efficiency I agree with you, but I wasn't aiming for that.
Perhaps I should have written a disclaimer.

Ted
 
B

Ben Morrow

Quoth Ted Zlatanov said:
I know this. I prefer to be explicit. I also say "shift @ARGV" in
the main body, so it's clear what I'm shifting.

It is clear what you're shifting: an unqualified 'shift' shifts off the
current argument list (in all cases). Writing the @_ explicitly simply
causes someone who knows Perl to have to do a double-take and verify
that the code is in fact shifting @_.
Great. My style is to do one per line, so I can comment each one
separately. I am aware of the (very minor) penalty, I think clear
comments are worth the sacrifice.

If you need to comment every parameter of a function (except in very
special cases) then you really need to give your variables better names
:). A comment (or, better, POD) above the function explaining what
parameters it takes and what they signify is, of course, a good idea.
OTOH, putting every parameter on its own line with its own bit of '=
shift @_;' boilerplate makes it much harder to see what the function is
actually doing.
I think my function is more suitable for a beginner example. The
function you show is absolutely terrible for that purpose IMHO. In
terms of efficiency I agree with you, but I wasn't aiming for that.

Efficiency has nothing to do with it: Anno's version is much clearer. I
would perhaps agree that for a beginner who has some experience with
C-like or Pascal-like languages an explanation of what map does in terms
of a loop might be helpful, but if we wish to teach people to write Perl
as opposed to C-as-a-subset-of-Perl then this is a very good example of
when map makes things substantially simpler. (Not that there's anything
*wrong* with writing C-as-a-subset-of-Perl if that's all someone is
prepared or able to learn, but I would have thought that in this group
we should be encouraging people to learn to use Perl properly. Otherwise
they'll never love it :).)

Ben
 
T

Ted Zlatanov

It is clear what you're shifting: an unqualified 'shift' shifts off the
current argument list (in all cases). Writing the @_ explicitly simply
causes someone who knows Perl to have to do a double-take and verify
that the code is in fact shifting @_.

I also use single-space indents, do I have to change that too? I
really think you and Anno are nit-picking style here. Please let me
write out "shift @_". It amuses me and harms no one.
putting every parameter on its own line with its own bit of '= shift
@_;' boilerplate makes it much harder to see what the function is
actually doing.

Well, I guess we'll have to disagree on this. Let's drop it, please.
Efficiency has nothing to do with it: Anno's version is much clearer.

You've got to be kidding. It mashes map without a clear code block
demarcation, uses a range with a recalculated end point on the fly,
and doesn't use return. All in a single line. I'm not criticing
*Anno's code*, just pointing out that for that to be clearer than my
while/push/return code *to a beginner*, you'd have to be on several
medications, none sold over the counter.
I would perhaps agree that for a beginner who has some experience
with C-like or Pascal-like languages an explanation of what map does
in terms of a loop might be helpful, but if we wish to teach people
to write Perl as opposed to C-as-a-subset-of-Perl then this is a
very good example of when map makes things substantially
simpler. (Not that there's anything *wrong* with writing
C-as-a-subset-of-Perl if that's all someone is prepared or able to
learn, but I would have thought that in this group we should be
encouraging people to learn to use Perl properly. Otherwise they'll
never love it :).)

I think Anno's code is clever, correct, fun, but not something I'd
want to see in a production environment or shown to a beginner.

I think you and many posters here like clever code that accomplishes
several things in one shot. That's great, but please don't push that
on beginners who don't yet know what an anonymous code block does.
Please don't assume it's "proper" Perl, either. It's fine Perl, but
as we all know there is no "proper" way to do things in Perl,
TMTOWTDI.

I noticed mine is buggy, I forgot to check that $inc is greater than
zero. It will loop forever otherwise. Sorry about that. Anno's code
will just die if $inc is zero, which is definitely nicer to the user.

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top