Re: [SPOILER] Perl Quiz of the Week #24 (Turing Machine simulation)
Daniel Martin <martin-+m399P62/[email protected]> Sat, 18 Sep 2004 23:29:19 -0400
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
Quoting "Michael C. Toren" <[email protected]>: > While using your implementation to debug Turing programs, I discovered an > additional small bug. If in a given state there is no state table entry > matching the tape cell under the head, the program exits as expected, but > only after erasing the cell (setting it to undef). For example, if the > Turing program: > > A 1 B 1 L > > is given an initial tape of "0", it will output "" rather than "0". One > possible workaround is: Note that it gets even worse if there's trailing underscores - this program, with a blank initial tape, shows a problem as well: a _ b 1 R b _ c _ R c _ d _ R d _ e _ R e X stop X R That should print "1", but mjd's original prints "1___". And I personally think it's easier, and the result is more readable, to make this change: 24c24 < while (my $ttab = $transition{$STATE}) { --- > while (my $ttab = $transition{$STATE}{$TAPE[$HEAD]}) { 37c37 < ($STATE, $TAPE[$HEAD], $inc) = @{$ttab->{$TAPE[$HEAD]}}; --- > ($STATE, $TAPE[$HEAD], $inc) = @{$ttab}; But yes, the debugging mode is nice - without it, I would have given up on the parens program when my first attempt failed. (Though I only had 5 bugs to track down, and 4 of those were transcription errors from the stack of paper where I'd worked it out to begin with. This is more a reflection of the fact that regexp substitutions often translate easily to Turing machines than anything else) -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/