Click here to get back home

Use of "return" in place of "last" (newbie question)?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Use of "return" in place of "last" (newbie question)? Michael 03-09-2008
Posted by Michael on March 9, 2008, 1:30 pm
Please log in for more thread options
On Sun, 09 Mar 2008 17:14:19 +0000, Jürgen Exner wrote:

>>I am new to perl, and I am wondering whether it is a bad thing to jump
>>out of a foreach loop with return.
>
> Technically it doesn't make a difference.
>
> Stylistically it is a matter of which programming philosophy you are
> following. For GOTOists it is standard operating procedure, for
> functional programmers it is a sacrilege. Others are somewhere in
> between.

Thank-you.

Just as a matter of interest, so far as I could see, my technique of
setting a flag was the simplest way of maintaining the integrity of the
foreach (terminating it with "last" rather than jumping out of it with
"return"), whilst not wasting processor cycles performing irrelevant loops
later in the function. I am a newbie and don't know much - is there a
more elegant alternative which would satisfy the functional programmers?


Posted by Joost Diepenmaat on March 9, 2008, 1:56 pm
Please log in for more thread options

> On Sun, 09 Mar 2008 17:14:19 +0000, Jürgen Exner wrote:
>
>>>I am new to perl, and I am wondering whether it is a bad thing to jump
>>>out of a foreach loop with return.
>>
>> Technically it doesn't make a difference.
>>
>> Stylistically it is a matter of which programming philosophy you are
>> following. For GOTOists it is standard operating procedure, for
>> functional programmers it is a sacrilege. Others are somewhere in
>> between.
>
> Thank-you.
>
> Just as a matter of interest, so far as I could see, my technique of
> setting a flag was the simplest way of maintaining the integrity of the
> foreach (terminating it with "last" rather than jumping out of it with
> "return"), whilst not wasting processor cycles performing irrelevant loops
> later in the function. I am a newbie and don't know much - is there a
> more elegant alternative which would satisfy the functional programmers?

There are more "elegant" (for some value of elegant) functional
alternatives , but perl's constructs don't really allow you to write
them as nicely as more functional programming languages do. For one,
perl's if { ... } else { ... } constructs aren't expressions, and also,
perl uses arrays instead of linked lists:

example in common lisp

(defun find-value (value list)
(and list
(if (string= value (first list))
value
(find-value (rest list)))))

(defun example
(or (find-value my-value my-list)
(find-value my-other-value my-other-list)))

Using these kind of recursive calls almost automatically
force you to break up your sample problem into 2 subs.

Also note that you can then re-use the find-value sub to reduce the
total amount of code.


same algorithm in perl.

sub find_value {
my ($value,@list) = @_;
if (@list) {
my $first = shift @list;
if ($first eq $value) {
return $first;
}
return find_value($value,@list);
}
return; # return false
}

sub example {
find_value($value,@list) or
find_value($other_value,@other_list);
}

note that you probably still want to use plenty of return()s
here.

also note: is is pretty inefficient if @list is long. you'd
generally write the find_value function using foreach(), and pass
the lists as array references.

ps: none of this code has been tested. YMMV.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by Michael on March 9, 2008, 5:15 pm
Please log in for more thread options
On Sun, 09 Mar 2008 18:56:51 +0100, Joost Diepenmaat wrote:

<snip>
>> Just as a matter of interest, so far as I could see, my technique of
>> setting a flag was the simplest way of maintaining the integrity of the
>> foreach (terminating it with "last" rather than jumping out of it with
>> "return"), whilst not wasting processor cycles performing irrelevant
>> loops later in the function. I am a newbie and don't know much - is
>> there a more elegant alternative which would satisfy the functional
>> programmers?
>
> There are more "elegant" (for some value of elegant) functional
> alternatives , but perl's constructs don't really allow you to write
> them as nicely as more functional programming languages do. For one,
> perl's if { ... } else { ... } constructs aren't expressions, and also,
> perl uses arrays instead of linked lists:
>
> example in common lisp
>
> (defun find-value (value list)
> (and list
> (if (string= value (first list))
> value
> (find-value (rest list)))))
>
> (defun example
> (or (find-value my-value my-list)
> (find-value my-other-value my-other-list)))
>
> Using these kind of recursive calls almost automatically force you to
> break up your sample problem into 2 subs.
>
> Also note that you can then re-use the find-value sub to reduce the
> total amount of code.
>
>
> same algorithm in perl.
>
> sub find_value {
> my ($value,@list) = @_;
> if (@list) {
> my $first = shift @list;
> if ($first eq $value) {
> return $first;
> }
> return find_value($value,@list);
> }
> return; # return false
> }
>
> sub example {
> find_value($value,@list) or
> find_value($other_value,@other_list);
> }
>
> note that you probably still want to use plenty of return()s here.
>
> also note: is is pretty inefficient if @list is long. you'd generally
> write the find_value function using foreach(), and pass the lists as
> array references.
>
> ps: none of this code has been tested. YMMV.

Thank-you very much for taking the time to write so helpfully Joost.

/M


Posted by Jürgen Exner on March 9, 2008, 3:49 pm
Please log in for more thread options
>On Sun, 09 Mar 2008 17:14:19 +0000, Jürgen Exner wrote:
>
>>>I am new to perl, and I am wondering whether it is a bad thing to jump
>>>out of a foreach loop with return.
>>
>> Technically it doesn't make a difference.
>>
>> Stylistically it is a matter of which programming philosophy you are
>> following. For GOTOists it is standard operating procedure, for
>> functional programmers it is a sacrilege. Others are somewhere in
>> between.

>Just as a matter of interest, so far as I could see, my technique of
>setting a flag was the simplest way of maintaining the integrity of the
>foreach (terminating it with "last" rather than jumping out of it with
>"return"), whilst not wasting processor cycles performing irrelevant loops
>later in the function. I am a newbie and don't know much - is there a
>more elegant alternative which would satisfy the functional programmers?

Well, doing strict functional programming in Perl is extremely hard anyway
because that't not how the language was designed.
But if you follow structured programming/formal programming/program
transformations/... then the condition under which the loop terminiates
should be defined in the condition of the loop. Neither last() nor return()
fits that bill because they exit the loop unexpectedly from somewhere in the
middle which is a nightmare for e.g. formal program verification.

Using tons of flags is the typical workaround by inexperienced programmers.
I have seen that literally hundreds of times in our university classes.
Unfortunately flags can easily lead to to convoluted and difficult to
maintain code because it can become very difficult to follow their logic and
to analyse where which flag is being set under what conditions.

Very often it takes just a little bit of restructuring of the loop to get a
clean and easy to understand single exit condition without bail-outs in the
middle. One approach that works often is to rearrange the exection sequence
in the loop and/or reduce the loop by one iteration by moving some
initialization code before the loop:

Generic example:
        while (cond_x) {
                do_some_work();
                if (cond_y) ;
                do_more_work();
        }

can usually be transformed into something like
        do__prep_work(); #includes do_some_work() for first iteration
        while (cond_x and not cond_y) {
                do_more_work();
                do_some_work();
        }
                
jue
                


IMO the better app

Posted by Michael on March 9, 2008, 5:14 pm
Please log in for more thread options
On Sun, 09 Mar 2008 19:49:41 +0000, Jürgen Exner wrote:
<snip>
>>Just as a matter of interest, so far as I could see, my technique of
>>setting a flag was the simplest way of maintaining the integrity of the
>>foreach (terminating it with "last" rather than jumping out of it with
>>"return"), whilst not wasting processor cycles performing irrelevant
>>loops later in the function. I am a newbie and don't know much - is
>>there a more elegant alternative which would satisfy the functional
>>programmers?
>
> Well, doing strict functional programming in Perl is extremely hard
> anyway because that't not how the language was designed. But if you
> follow structured programming/formal programming/program
> transformations/... then the condition under which the loop terminiates
> should be defined in the condition of the loop. Neither last() nor
> return() fits that bill because they exit the loop unexpectedly from
> somewhere in the middle which is a nightmare for e.g. formal program
> verification.
>
> Using tons of flags is the typical workaround by inexperienced
> programmers. I have seen that literally hundreds of times in our
> university classes. Unfortunately flags can easily lead to to convoluted
> and difficult to maintain code because it can become very difficult to
> follow their logic and to analyse where which flag is being set under
> what conditions.
>
> Very often it takes just a little bit of restructuring of the loop to
> get a clean and easy to understand single exit condition without
> bail-outs in the middle. One approach that works often is to rearrange
> the exection sequence in the loop and/or reduce the loop by one
> iteration by moving some initialization code before the loop:
>
> Generic example:
>         while (cond_x) {
>                 do_some_work();
>                 if (cond_y) ;
>                 do_more_work();
>         }
>
> can usually be transformed into something like
>         do__prep_work(); #includes do_some_work() for first iteration while
>         (cond_x and not cond_y) {
>                 do_more_work();
>                 do_some_work();
>         }

Thank-you very much for taking the time to write so helpfully Jue.

/M

Similar ThreadsPosted
If you are looking for a place to Buy and sell your used stuff and promote your small business..here is the place to go July 19, 2008, 3:55 am
Perl newbie - getting "system()" to return January 31, 2005, 4:05 am
MS Perl question -- how to use hacked script to work correctly(was Question on loops and return values or sumpin) December 8, 2004, 12:59 pm
Question on loops and return values December 3, 2004, 3:02 pm
multiple return regex question December 11, 2007, 4:04 pm
Return Codes in Perl: Question on implementing good patterns April 27, 2005, 9:32 am
newbie question September 12, 2004, 11:06 am
A newbie question October 14, 2004, 7:51 pm
Newbie Question November 6, 2004, 6:10 am
newbie-ish question November 18, 2004, 3:53 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap