Higher-Order Perl Errata

For 2nd printing (1st printing, what?)

Last update: 23 March 2014

Listing by severity

Listing by contributor | Listing by date reported | Listing by page number

Substantial technical errors

222-223

The output produced by the make_partition() functions is not as is shown on page 223; the iterators generated by the code traverse the search space in depth-first order, but the sample output on page 223 is in breadth-first order. To fix this, change

        my $item = pop @agenda;

to

        my $item = shift @agenda;

in each function. The discussion in the first paragraph on page 223 is confused. It says:

Because we return each partition immediately, after putting its children onto the agenda, old nodes are never preempted by new ones, regardless of whether we use pop or shift.

This is completely wrong. When pop is used, nodes are taken from the end of the agenda; the nodes so taken are the ones most recently added, so they certainly do pre-empt the nodes that were on the agenda before.

(DG)

308-310

The discussion of the tortoise-and-hare algorithm suggests that it can detect repeating values in a sequence without the memory costs of maintaining a record of the already-seen values. In most contexts, this is true. However, in the context of infinite streams, the tortoise-and-hare algorithm loses this benefit. After n iterations, the tortoise is at the nth stream element and the hare is at the 2nth. Therefore, there are n stream elements held in memory that will not be garbage-collected until the tortoise passes them.

Since the memory cost is proportional to n anyway, the simple, obvious method is probably better:

        sub cut_loops {
          my ($stream, $seen) = @_;
          $seen = {} unless defined $seen;
          my $nextval = drop($stream);
          return if $seen->{$nextval}++ ;
          return node($nextval, promise { cut_loops($stream, $seen) });
        }

The tortoise-and-hare algorithm does make sense in the context of the iterators of chapters 4 and 5.

466

Using _ as the name of a subroutine is a bad idea. One reason is that the name _ is special, and is always forced into package main::. So for example:

        package Parser;

        sub _ { die "Ouch" }

        package Unrelated;

        $dir_readable = -d $dir && -r _ ;

does not do what you expect; you want _ to be interpreted as the name of a filehandle, but it is not. You have defined a subroutine named main::_ here, not Parser::_. Then, in the unrelated package, you think that in -r _ the _ is the name of a filehandle. Usually it is, and it usually names the special filehandle main::_. But now the Perl parser interprets it as a call to the function main::_(), and the statement dies.

Moreover, Wolfgang Laun points out that _ cannot be exported in the normal way, because of its special interaction with namespaces. For example, suppose you try to do:

        package F;
        use base 'Exporter';
        BEGIN { @EXPORT = '_' }

        sub _ { 
          print "_(@_)\n" 
        }

        package main;
        F->import();

        _(1..3);

Here we are trying to export _ from package F:: to package main::. But there is no _ in package F::; it is already in main::! So you get

        Subroutine main::_ redefined at /usr/local/lib/perl5/5.8.0/Exporter.pm line 59. at ...
        Undefined subroutine &F::_ called at ...

If you change the code to

        package F;
        use base 'Exporter';
        BEGIN { @EXPORT = '_' }

        sub F::_ {
          print "_(@_)\n" 
        }

        package main;
        F->import();

        _(1..3);

then it works as it should, for some value of "should". As far as I know HOP does not contain an instance of this error.

Wolfgang Laun points out other syntactic problems that are not exercised in HOP but that support the general conclusion that my attempt to use the compact notation

        _"...";

was a bad idea. The following expressions are syntax errors:

        _'arg'
        &_'arg'
        &_ 'arg'

although

        _ 'arg'

is acceptable. This is not actually a problem with _; it occurs because ' is still available as an obsolete synonym for ::. (This causes problems elsewhere; for example $x = "Fred"; print "$x's wallet" does not print "Fred's wallet".) On page 555 I used

        $base_name = _"IDENTIFIER";

which does work, but if I had used single quotes, it would have failed. Unlike the two problems described above, changing _ to __ will not fix this, because the problem is really with the ' symbol.

(GY)

502-512

The Gaussian elimination technique for solving linear equations is notoriously unreliable in conjunction with floating-point numbers, and has a number of dangerous behaviors. I chose to use it anyway because it is a lot easier to understand than any other method I could have presented.

In this application, however, I have encountered at least one serious problem with the algorithm as presented in the book. Under some circumstances, the equation solver will fail to eliminate a variable from an equation when it should have. For example, suppose one equation is

        x + 2y + 2z            = 9

and another is

            2y + 2z            = 0

and the program uses the second equation to eliminate y from the first equation; the result should be:

        x                      = 9

But now suppose that because of an earlier floating-point roundoff error, the second equation is instead:

            2y + 1.9999999999z = 0

Then after eliminating y from the first equation, one has

        x +       .0000000001z = 9

If no other equations involve z, the erroneous z term will never be eliminated from this last equation, and the program will incorrectly conclude that it cannot determine the value for x.

For linogram, I think a suitable fix is to change the arithmetic() function on page 504:

        sub arithmetic {
          my ($a, $ac, $b, $bc) = @_;
          my %new;
          for my $k (keys(%$a), keys %$b) {
            my ($av) = $a->coefficient($k);
            my ($bv) = $b->coefficient($k);
            $new{$k} = $ac * $av + $bc * $bv;
          }
          $a->new(%new);
        }

We will add a special case that watches out for floating-point dust:

        my $EPSILON = 1e-6;

        sub arithmetic {
          my ($a, $ac, $b, $bc) = @_;
          my %new;
          for my $k (keys(%$a), keys %$b) {
            my ($av) = $a->coefficient($k);
            my ($bv) = $b->coefficient($k);
            my $new = $ac * $av + $bc * $bv;
            $new{$k} = abs($new) < $EPSILON ? 0 : $new;
          }
          $a->new(%new);
        }

If a new coefficient is calculated to be too small, we assume it is due to round-off error, and truncate it to zero. I think this is probably safe, because values in linogram nearly always represent distances in a diagram, and output devices don't have sufficient resolution to render meaningful differences on the order of $EPSILON. This change may, of course, introduce new problems. A safer alternative might be to use rational arithmetic throughout.

Minor substantive errors

21

The line

           warn "Couldn't open directory $code: $!; skipping.\n";

should read

           warn "Couldn't open directory $top: $!; skipping.\n";

(The same error also appears on page 24 in the first printing.)

(JPL)

33

The call to promote_if() at the top of the page has:

        promote_if(
           sub { $_[0] eq 'h1' },
           $_[0])

but it should be:

        promote_if(
           sub { $_[0] eq 'h1' },
           @_)

This erroneous code also appears at the top of page 327.

(JCZ)

94

The discussion of the find_share() function gets the two arguments in the wrong order. Although some of these were corrected in the second printing, one was not. Three lines before the section break, find_share([1..20], 210) should actually be find_share(210, [1..20]).

(DFC)

123-124

The else on page 124 has no associated if. The while on page 123 should be an if, and the lines:

        if (-d $file) {
          opendir my $dh, $file or next;

on page 123 should read:

        if (-d $file && opendir my $dh) { 

instead.

(DG)

129

The next-to-last paragraph says that items are removed from @items and placed onto the end of @perms. Actually the code has unshift, so they're placed at the beginning of @perms, not the end.

(VL)

132-133

In increment_odometer(), the line

        until ($odometer[$wheel] < 9 || $wheel < 0) {

should perform the tests in the other order:

        until ($wheel < 0 || $odometer[$wheel] < 9) {

It is not logically correct to use $wheel as an array subscript when it might be out of range. The function works as printed, but this is something of a lucky fluke.

An analogous problem is present in increment_pattern() on page 133.

135

The phrase "DNA is organized as a sequence of base pairs..." should read "DNA is organized as two complementary sequences of nucleotides...". A "base pair" is one complementary pair of nucleotides.

140

The FlatDB library described starting on this page uses the Iterator() function defined elsewhere in the Iterator_Utils package, so it needs to import that function. The line

         use Iterator_Utils qw(Iterator);

should be added just after the package FlatDB; declaration.

(ANON)

232

In the middle of the last paragraph, the phrase "rebinding the variables to the appropriate variables" should be "rebinding the variables to the appropriate values".

348

In the first version of fold(), at the top of the page, the line:

        $fold->($f->($x, $first), @_)

should read:

        $fold->($f->($x, $first))->(@_)
(GM)

407

In the middle of the page, the line

        my ($plus_token, $term_value, RIGHT) = @_;

is missing the $ on RIGHT.

Also, $plus_token should be $minus_token.

(MK)

474

The - node at the lower-right in the diagram (connected to wires m, n, and p) should be a + node, since k = c + 273.15. The diagram on page 486 gets it right.

Similarly, the reference to the node in the middle of the page ("...causing the - node to set wire p to 310.15...") should describe it as a + node instead of as a - node.

(WM)

477

The settor_is() function issues an inappropriate warning if it's called to check the settor of a node with no settor. The code has

        sub settor_is { $_[0]{S} == $_[1] }

but it would be better as:

        sub settor_is {
          my $settor = $_[0]{S};
          defined($settor) && $settor == $_[1];
        }

The function works properly without this change, except for the warning.

(VZ)

477

There are errors in the set() function on page 476 and the revoke() function on page 477. The value() and has_value() methods each require an argument, to inform the methods of who is asking the question. set() and revoke() don't supply this argument, which is mandatory.

In set() on page 476, the lines:

        unless ($value == $self->value) {
          my $v = $self->value;

should be:

        unless ($value == $self->value($settor)) {
          my $v = $self->value($settor);

and similarly, in revoke() on page 477, the line:

        return unless $self->has_value;

should be removed entirely; if the request to revoke the value was made by the only component that is allowed to revoke it, namely the one that set the value in the first place, then has_value() will return false.

Without these changes, programs generate a lot of (inappropriate) "use of uninitialized value" warnings, and may produce incorrect results under some circumstances.

485

The letter labels in the program code for the local propagation network are intended to match the ones in the diagram on page 483. But the labels $i and $j are switched, so the network that is constructed by the code is wired up wrong, and the network produces wrong answers. Instead of:

        { my ($i, $j, $k, $l, $m) = Wire->make(5);
          $F = new_io('Fahrenheit', $i);
          $C = new_io('Celsius', $m);
          new_constant(32, $j);
          new_constant(5/9, $l);
          new_adder($j,$k,$i);
          new_multiplier($k,$l,$m);
        }

one should have:

        { my ($i, $j, $k, $l, $m) = Wire->make(5);
          $F = new_io('Fahrenheit', $j);
          $C = new_io('Celsius', $m);
          new_constant(32, $i);
          ...

Alternatively, just change new_adder to new_subtractor on line 6.

(VZ)

492

The display in the top half of the page of "four mathematically equivalent forms" contains one form that isn't equivalent. The four forms are

                     F + (hspc, 0) = plus;
                     plus + (-hspc, 0) = F;
                     plus - F = (-hspc, 0);
                     plus - (hspc, 0) - F = 0;

but the third one should be

                     plus - F = (hspc, 0);

instead.

Trivial errors (typography, spacing, etc.) that affect the execution of code

32

The second boldface line in promote_if() is missing a parenthesis; it says

        if ($is_interesting->($element->{_tag}) {

but it should be

        if ($is_interesting->($element->{_tag})) {
(JCZ)

34

The definition of the Fibonacci function here is not the one that is usually used by mathematicians. Mathematicians always put fib(0) = 0 and fib(1) = 1, so fib(2) = 1 also. But the definition I give on page 34 has fib(0) = 1 instead, so that fib(2) = 2.

This may have been deliberate, and in any case the error is small, because page_34_fib($n) = standard_fib($n+1) for all n. But it at least shows poor judgement, because the definition I did use was inconsistent with the table on page 33, which has fib(1) = fib(2) = 1 as is standard.

If one uses the standard definition, the Fibonacci function enjoys a number of mathematical properties that make its analysis more tractable. (For example, fib($a*$n) is always an exact multiple of fib($n).)

It would have been better to use the usual definition, as I did on page 243:

        sub fib {
          my $n = shift;
          if ($n < 2) { return $n }
          fib($n-2) + fib($n-1);
        }

The nonstandard definition also appears on pages 66 and 69 and in the illustration on page 76.

(JCZ)

49

There is a } missing from the last line on the page.

(JPL)

59

The _DEFAULT_ callback function is missing its close brace.

(JCZ)

59

In the code display in the middle of the page, the expression $table{_DEFAULT_} should be $table->{_DEFAULT_}.

(JCZ)

110

In the fourth paragraph, the phrase "the cost-benefit ratio is nine times as large" should read "the cost-benefit ratio improves ninefold".

128

The loop at the top of the page:

        while ($file = NEXTVAL($octopus_file)) { ... } 

will terminate prematurely if the initial call to interesting_files() includes a request to examine a file named 0. To avoid this edge case, one should write:

        while (defined($file = NEXTVAL($octopus_file))) { ... } 

instead. Similarly, the if test just below should be if (defined(NEXTVAL(...))) rather than just if (NEXTVAL(...)).

(JPL)

128

The variable $next_octopus in the second code display should have been $octopus_file, to be consistent with the first code display and with the example at the bottom of the previous page.

(JPL)

136

The make_genes() function does not correctly handle the special case where the input contains an empty wildcard, symbolized by (). In such a case, there are no matching strings, and the iterator should immediately indicate exhaustion.

(JPL)

152

In the first code display, the line

        open my($fh), "|-", "tac", $file
          or return;

should be

        open my($fh), "-|", "tac", $file
          or return;
(JCZ)

152

At the bottom of the page, the line

    my $q = $db->callbackquery(sub {my %F=@_; $F{PAGE}=~ m{/book/$}});

should be

    my $q = $db->callbackquery(sub {my %F=@_; $F{page}=~ m{/book/$}});

because the keys in the %F hash must match the names passed in the call to new on the previous line.

(JCZ)

158-160

Each of the three pages has a call to imap that divides numbers by 37268. Each division should be by 32768 instead.

(IS)

224

In the (perhaps unlikely) event that Perl's built-in sort function tries to use the partitions() comparator to compare an element with itself, the comparator will return an undefined value, which is a fatal error. For more robustness, add return 0; after the for loop.

(JPL)

237

The phrase "rewrite the while loop as a for loop" should probably be "rewrite the until loop as a for loop".

(VL)

291

In the cutsort() function, I wish that instead of this:

        insert(@pending, head($s), $cmp);
        $s = tail($s);

I had written this:

        insert(@pending, drop($s), $cmp);

A similar kind of change is possible in display_failures() on page 433, although less obviously preferable.

310

The name of the function should be cut_loops, not cut_loops2. Otherwise, the recursive call inside the function won't work properly.

(MB)

388

In the version of alternate() at the top of the page, the lines:

        if (($v, $newinput) = $p1->($input)} { return ($v, $newinput) }
        if (($v, $newinput) = $p2->($input)} { return ($v, $newinput) }

should be:

        if (($v, $newinput) = $p1->($input)) { return ($v, $newinput) }
        if (($v, $newinput) = $p2->($input)) { return ($v, $newinput) }

493

In the display midpage, with examples of hidden constraints, one of the constraints is:

        top.start + wd = top.end

which does not make sense, since top.start is a point and wd is a scalar. It should have said:

        top.start + (wd, 0) = top.end

Similarly, the analogous constraint at the bottom of the page should be fixed analogously, to say:

        F.top.start + (F.wd, 0) = F.top.end

Trivial errors (typography, spacing, etc.) that don't affect the execution of code

21

In the last clause of the second paragraph, the phrase "all it knows is that is should..." should read "all it knows is that it should...".

(KS)

24

In the middle display, there is a space missing between push and @results, and there is a space missing before the colon in the final return statement.

(JK)

28

In the second display, the closing ] is indented too far.

(JK)

32-33

The last code display on page 32, and the first on page 33, use tags 'maybe', which is inconsistent with the all-capitals tags used on page 30-32. Although I think that the code will still run as given, there was no reason to change the tags; it was just an oversight.

A similar inconsistency appears on pages 326-328.

53

At the top of the page, the phrase "if looks for" should be "it looks for".

(TJH)

57

In the last display, the next-to-last } is indented too far.

(JK)

64

In the final paragraph, the fonts in the phrase "with arguments 128,0,64" are wrong. It should appear as "with arguments (128, 0, 64)".

76

In the illustration, the code for the caching stub is missing a $ sign. The second line has func->(@_), but it should have $func->(@_).

(LE)

79

In the middle of the page, there is a space missing between "in" and "Figure 3.6".

130

There is an extra blank line before the closing brace of the while loop in the middle of the page.

(JPL)

135

The spacing in the first paragraph of section 4.3.2 is generally too tight, and in particular the words "of" and "Virginia" in the first sentence are too close together.

(VL)

139

The second sentence of section 4.3.3 says "Why would we want to this?", but it should be "Why would we want to do this?".

(JCZ)

143

In the code display in the middle of the page, the line:

        my $q = $dbh->callbackquery(sub { my %F=@_; $F{STATE} eq 'NY'});

is missing a space:

        my $q = $dbh->callbackquery(sub { my %F=@_; $F{STATE} eq 'NY' });

180-181

On each page, there is an extra space in the line:

        return ()  if $cur_elt >= $stop_size;
(JPL)

216

There's an extra space after the % sign in n % 2 == 0.

(JPL)

217

The third sentence on the page is missing a word. It should say "...when the call returns, it moves back up to the parent."

(JCZ)

228

The -- in the code display in the middle of the page is in the wrong font.

(JPL)

228

The close brace in the display in the middle of the page is indented one space too far.

(JPL)

236

The last line of the code on the page, $i++;, is indented one space too far.

260

In the middle of the page,

        node($m, upfrom_list($m+1) );

should be

        node($m, upfrom_list($m+1));

275

In the code display, the last line, which is output of the program, should be in boldface.

326

There should be a blank line between the two function definitions in the first display.

326-328

The 'keeper' and 'maybe' tags are lowercase, which is inconsistent with the usage established on pages 30-32. See the correction for pages 32-33 for more information.

329

In the first code display, the longest line should end with )) }); instead of with ))}); .

377

At the bottom of the page, the phrase "one if its productions" should be "one of its productions".

(TJH)

390

The first complete sentence on the page has a dangling pronoun. It says "But it will be less efficient than the previous version, because it will call alternate() and concatenate() each time it is called...". The word it here refers to the parser constructed by star(), not to star() itself, and so should read "But this will be less efficient than the previous version, because the constructed parser will call...".

429

In the code display at the bottom of the page, the apostrophes should be neutral single quotes.

480-485

Most of the code on these pages should be tagged as "Code library", downloadable from http://hop.perl.plover.com/Examples/Local_Propagation.pm. The example on page 485 should be tagged as downloadable from http://hop.perl.plover.com/Examples/local-propagation.

500

In the last paragraph on the page, there is an extra comma after the phrase "The values 14, 9, and 3.5".

502

In the first line on the page, the semicolon should be a colon.

509

Near the bottom of the page, the phrase "one if its variables" should read "one of its variables".

525-526

The two displays on each page should be aligned.

544

The definition of $definition has

        $definition = labeledblock($defheader, $Declaration) ...

This will work, as long as $defheader is already properly defined, which it is. But for consistency, we should probably write this instead:

        $definition = labeledblock($Defheader, $Declaration) ...
(PW)

544

In the middle of the page, the phrase "$extends is $line" should be "$extends is line".

(TJH)

545

In the second paragraph, "Two others are..." should be "The two others are...".

(PW)

546-547

The phrases (expression representing constant 3) and (expression representing variable 'boxwid') should be in a slanted monospace font.

551

NAME => 'TOP' should read NAME => 'top'.

557

The two lines that read

        age = 4;

should read

        constraints { age = 4; }

instead.

559

In the last paragraph, "named" should be "names".

573

The line under "Unix" that mentions "epoch time format" should be indented.

575

The function index entry for alternate2 is improperly run in to the preceding entry.

Non-errors

(But possibly confusing anyway)

49

Note that this version of read_config() doesn't support the INCLUDE directive as defined in the previous section, because order of the arguments in the $action call is inconsistent with the order of the parameters in the read_config() function. A similar problem occurs with the version on page 50.

If you want to fix this, one way is to reorder the parameters; another is to have INCLUDE call a helper function that calls read_config() in turn with the arguments in the correct order.

(TH)

139

The code

     for my $abbrev (keys %n_expand) {
       $pat =~ s/$abbrev/($n_expand{$abbrev})/g;
     }

would have been simpler as

     while (my ($abbrev, $expand) = each %n_expand) {
       $pat =~ s/$abbrev/($expand)/g;
     }
(GC)

180-181

The version of each_array() on page 180 uses ref $arrays[0] to test the first argument; if it is a non-reference, it is taken to be the label of a stop-type function. In the version on page 181, this test has changed to UNIVERSAL::isa($arrays[0], 'ARRAY'). That's because the page 181 version is expecting that the first argument might be a code reference, so the simpler ref test will no longer work.

(GC)

235

The first line in the powerset_recurse function reads

        my ( $set, $powerset, $keys, $values, $n, $i ) = @_;

but it should read

        my ( $set, $powerset, $keys, $values, $nmembers, $i ) = @_;

This wasn't originally my mistake; I took the code as it was written in Mastering Algorithms with Perl.

(LSL)

278-279

The boxes labeled MERGE should probably say UNION instead, since what's being used here is actually the union() function of p.274, and the behavior is different from that of the merge() function of p.271 that was mentioned in the analogous diagram on p.272.

(JPL)

291

It may appear that list_to_stream() is broken, because, for example:

        my $list = list_to_stream( 1 .. 10 );
        1 while defined drop($list);

fails; under strict 'refs' it says Can't use string ("10") as an ARRAY ref. But no, the function is working as designed and as documented on pages 291--292. The final argument of list_to_stream() is required to be the "final tail" of the stream that is constructed, and must be "another (possibly empty) stream or a promise"; in this example it is neither. The correct call would be:

        my $list = list_to_stream( 1 .. 10, undef );

where the final undef argument is an empty stream, as in the example on page 292.

(O)

295

The line

        tail(iterate_function(sub { _next_record($fh, $filename, $devino) }));

is designed to work with the iterate_function() function that is defined on page 263; this version returns a stream. There is another, different iterate_function() function on page 134, that returns a chapter-4-style iterator object; that won't work here. I should have given the two functions different names, but sometimes these things slip through the editing process.

(HDP)

303

Using a fixed constant like 1e-12 in close_enough() is a bad idea; one should calculate error relative to the size of the arguments. I didn't mean to suggest that one should use this exact code; it was intended only as an example. A similar consideration holds for the value of $e on page 305.

(WL)

305

See page 303.

(WL)

436-437

Regarding the part of the lexer that handles hex escapes, which has

        \\x[0-9a-fA-F]{0,2}

Per Westerlund asks "I wonder if the intention was to allow hex escapes with 0, 1, or 2 following hex digits? What does \x mean in this context?"

That's a good question! It is intentional; I carefully checked the behavior of the regex engine while I was writing this section. In the construction \x0a, the leading zeroes are optional, and in fact they're so optional that you can write \x to mean \x00. I probably wouldn't want to use this feature in a real program, but in this case we might as well do what the real regex engine does.

(PW)

538

In add_constraints(), it might look as though this:

        push @{$self->{C}},
          $value->intrinsic->constraints,
          $value->synthetic->constraints;

should be this:

        push @{$self->{C}},
          $value->intrinsic_constraints,
          $value->synthetic_constraints;

but it's not so. The intrinsic_constraints() method is called on a Type object to calculate its basic set of intrinsic constraints from scratch, but the $value here is not a Type object but a Value::Feature object. intrinsic() is an accessor that returns the intrinsic constraint object already calculated for this expression, and constraints() extracts the list of constraints from this object. Similarly for the synthetic constraints.

(PW)


Thanks

ANON Anonymous contributors JCZJean-Christophe Zeus O Ovid
DFCDaniel Frederick Crisman JKJim Kovacs PWPer Westerlund
DGDavid Golden JPLJohn P. Linderman THThomas Herchenroeder
GCGustavo Chaves KSKripa Sundar TJHJiahai Teng
GMGreg Matheson LELeif Eriksen VLVincent Lee
GYGaal Yahas LSLLuc St-Louis VZVyacheslav Zakovyrya
HDPHans Dieter Pearcey MBMartin Busik WLWolfgang Laun
ISImre Saling MKMatt Kraai WMWalt Mankowski

Return to: Universe of Discourse main page | What's new page | Perl Paraphernalia | Higher-Order Perl

mjd-perl-hop+@plover.com