Menu Categories Author

Nubis Novem

Consulting On Cloud Nine

Reading Higher-Order Perl: book link

A Perl book was highly recommended to us long time ago. We have decided to work through that and test-drive some of its code examples. Plan is to post more or less regular blog updates on our progress. Are you with us? Here is the book:

Higher-Order Perl
by Mark Jason Dominus
Published in 2005. Available for a free download from the author’s website.

Comments

(2)
  • Maksim Otstavnov
    #

    The first example (§ 1.1: Decimal to binary conversion) reads:

    sub binary {
    my ($n) = @_;
    return $n if $n == 0 || $n == 1;
    my $k = int($n/2);
    my $b = $n % 2;
    my $E = bin($k);
    return $E . $b;
    }

    I would put it something like this (sorry, I am not a real Perl gerl):

    sub binary {
    my ($n) = @_;
    $n == 0 || $n == 1
    ? $n
    : binary(int($n / 2)) . $n % 2
    };

    Ditto the second example (§ 1.2: Factorial) which I would implement in a similar way:

    sub factorial {
    my ($n) = @_;
    $n == 0
    ? 1
    : $n * factorial($n-1) # never miss a chance for tail recursion
    }

    Is defining extra variables (they are real variables, not just extra names for values, are they not?), using conditional statements instead of conditional expression, and using fancy control flows peculiar to Perl culture? Or just reminiscences of Basic heritage?

    • Andrei Spassibojko
      #

      One of the Perl’s inner beauties is the fact that identical functionality could be achieved by several different ways. It is also possible that would be true for most programming languages. Then style comes to mind, which is based on judgements of personal preferences (taste, comprehension) and formal criteria (efficiency, ease of maintenance). We think that book example should be easier to understand to a wider audience while your code snippets are more sophisticated and, possibly, more efficient.

Leave a Reply

Your email address will not be published. Required fields are marked *