Re: obj 8-2

Discussion in 'Perl Misc' started by Dave Saville, Feb 22, 2014.

  1. Dave Saville

    Dave Saville Guest

    On Sat, 22 Feb 2014 15:57:57 UTC, Marek Novotny <>
    wrote:

    >>

    > What is so different about these two lines of code that made that
    > difference?
    >
    > my $answer = 0;
    > my $answer = $array[0];


    In the first case you are setting $answer to an arbitrary value which,
    if you are looking for a maximum, needs to be *smaller* than anything
    in the data to be searched. 0 works for all positive numbers as all
    positive numbers are bigger than 0. But for negative numbers 0 is
    *bigger* than all of them - even if there is no 0 in the list as you
    found by removing it.

    In the second case you are starting with a value you *know* is
    actually in the list. It matters not what that value is - just that it
    is from the list. You could be really retentive and only search from
    the second [1] element onwards, but its hardly worth the bother to
    save one compare and an index increment.

    HTH
    --
    Regards
    Dave Saville
    Dave Saville, Feb 22, 2014
    #1
    1. Advertising

  2. "Dave Saville" <> writes:
    > On Sat, 22 Feb 2014 15:57:57 UTC, Marek Novotny <>
    > wrote:
    >>>

    >> What is so different about these two lines of code that made that
    >> difference?
    >>
    >> my $answer = 0;
    >> my $answer = $array[0];

    >
    > In the first case you are setting $answer to an arbitrary value which,
    > if you are looking for a maximum, needs to be *smaller* than anything
    > in the data to be searched. 0 works for all positive numbers as all
    > positive numbers are bigger than 0. But for negative numbers 0 is
    > *bigger* than all of them - even if there is no 0 in the list as you
    > found by removing it.
    >
    > In the second case you are starting with a value you *know* is
    > actually in the list. It matters not what that value is - just that it
    > is from the list. You could be really retentive and only search from
    > the second [1] element onwards, but its hardly worth the bother to
    > save one compare and an index increment.


    And that still misses another corner case, namely an empty list.

    Which raises the question: what is the maximum value of an empty list?
    Logically there is none, but in Perl undef is probably a reasonable
    answer -- which means that anything that uses this code will either have
    to be prepared to handle a result of undef, or will need to guarantee
    that the list is never empty.

    You could do it like this:

    my $answer = undef;
    foreach my $elem (@array) {
    if (not defined $answer or $elem > $answer) {
    $answer = $elem;
    }
    }

    but that means checking whether $answer is defined on each iteration,
    which seems wasteful. This is probably better:

    my $answer = undef;
    if (scalar @array > 0) {
    $answer = $array[0];
    foreach my $elem (@array) {
    if ($elem > $answer) {
    $answer = $elem;
    }
    }
    }

    Some might prefer to write "if (scalar @array > 0)" as "if (@array)".

    (Warning: I think the above code is correct, but I haven't tested it.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 24, 2014
    #2
    1. Advertising

  3. Keith Thompson <> writes:
    > "Dave Saville" <> writes:


    [...]

    >> In the second case you are starting with a value you *know* is
    >> actually in the list. It matters not what that value is - just that it
    >> is from the list. You could be really retentive and only search from
    >> the second [1] element onwards, but its hardly worth the bother to
    >> save one compare and an index increment.

    >
    > And that still misses another corner case, namely an empty list.
    >
    > Which raises the question: what is the maximum value of an empty list?
    > Logically there is none, but in Perl undef is probably a reasonable
    > answer -- which means that anything that uses this code will either have
    > to be prepared to handle a result of undef, or will need to guarantee
    > that the list is never empty.
    >
    > You could do it like this:
    >
    > my $answer = undef;
    > foreach my $elem (@array) {
    > if (not defined $answer or $elem > $answer) {
    > $answer = $elem;
    > }
    > }


    There's no need to special-case this. Assuming a function defined as

    sub max
    {
    my $m;

    $m = shift;
    $_ > $m and $m = $_ for @_;
    return $m;
    }

    or

    sub max
    {
    my $m;

    $m = $_[0];
    $_ > $m and $m = $_ for @_[1 .. $#_];
    return $m;
    }

    empty lists will be handled as desired (Since the list is empty,
    attempts to access its first element will return undef and a loop
    iterating over all elements or all remaining elements won't do
    anything).
    Rainer Weikusat, Feb 24, 2014
    #3
  4. Keith Thompson <> wrote:
    >And that still misses another corner case, namely an empty list.
    >
    >Which raises the question: what is the maximum value of an empty list?


    And exactly because of this any behaviour is acceptable in case of an
    emtpy list, including returning nothing, returning some random number,
    and even a runtime system error.

    >Logically there is none, but in Perl undef is probably a reasonable
    >answer -- which means that anything that uses this code will either have
    >to be prepared to handle a result of undef, or will need to guarantee
    >that the list is never empty.


    Yep. That function is only well defined in case of a non-empty list.
    Just like a division is only well defined in case of a non-zero devisor.

    jue
    Jürgen Exner, Feb 25, 2014
    #4
  5. Keith Thompson <> writes:

    [...]

    > my $answer = undef;
    > if (scalar @array > 0) {
    > $answer = $array[0];
    > foreach my $elem (@array) {
    > if ($elem > $answer) {
    > $answer = $elem;
    > }
    > }
    > }


    Something which also has to be mentioned in this context: It is
    generally prudent to write code such that it performs the actions it has
    to perform in order to achieve a particular end, not such that it
    performs all kinds of additional ones just because "that's less work and
    it surely won't hurt". At times, the latter approach has resulted in
    interesting fireworks,

    http://homepages.inf.ed.ac.uk/perdita/Book/ariane5rep.html

    For the given case, this would mean 'loop over the remainder of the array
    since the first element has already been dealt with'.
    Rainer Weikusat, Feb 25, 2014
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    3
    Views:
    15,682
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Phil Endecott

    Obj* ptr = new Obj(X)

    Phil Endecott, Jun 3, 2005, in forum: C++
    Replies:
    5
    Views:
    460
    Mark P
    Jun 3, 2005
  3. Shalabh Chaturvedi
    Replies:
    2
    Views:
    442
    Mike C. Fletcher
    Feb 20, 2004
  4. Matthew Thorley

    How do you convert a string obj to a file obj?

    Matthew Thorley, May 4, 2005, in forum: Python
    Replies:
    7
    Views:
    500
    Peter Otten
    May 4, 2005
  5. alf
    Replies:
    9
    Views:
    402
Loading...

Share This Page