Next | You can't get there from here | 26 |

What's wrong with this?

use Timeout;

sub halts { my ($source_code, $input) = @_; my $function = eval $source_code;

# Run the function for at most an hour my $finished = with_timeout 3600, sub { $function->($input) };

if ($finished) { return 1 } # It finished within an hour else { return 0 } # It was still running after an hour }

This doesn't always produce the

*right*answerConsider a function that takes 65 minutes to finish

Or one that takes 65 years

If wrong answers were allowed, this would be a simpler solution:

sub halts { my ($source_code, $input) = @_; return 1; # Yuk yuk yuk }

Next | Copyright © 2005 M. J. Dominus |