为什么我的 Perl 递归函数永远不会结束?

发布于 2024-09-24 12:30:56 字数 367 浏览 2 评论 0原文

我正在尝试编写以下递归函数。问题是它永远不会结束,我不明白为什么:

    sub do_smth(@first, @second){
    my @tmp_first = @first;
    $tmp = shift(@tmp_first);
    if (@tmp_first > 0){
        do_smth(@tmp_first, @second);
    }
    my @tmp_second = @second;
    $tmp = shift(@tmp_second);
    if (@tmp_second > 0){
        do_smth(@first, @tmp_second);
    }

}

I'm trying to write the following recursive function. The problem is that it never ends and I can't understand why:

    sub do_smth(@first, @second){
    my @tmp_first = @first;
    $tmp = shift(@tmp_first);
    if (@tmp_first > 0){
        do_smth(@tmp_first, @second);
    }
    my @tmp_second = @second;
    $tmp = shift(@tmp_second);
    if (@tmp_second > 0){
        do_smth(@first, @tmp_second);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铜锣湾横着走 2024-10-01 12:30:56

这段代码甚至无法编译。如果没有警告和严格,您将得到这些错误:

Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 5, near "$tmp_first)"
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 10, near "$tmp_second)"
Execution aborted due to compilation errors.

并且有警告和严格:

Illegal character in prototype for main::do_smth : @first,@second at so.pl line 4.
Global symbol "@first" requires explicit package name at so.pl line 5.
Global symbol "$tmp" requires explicit package name at so.pl line 6.
Global symbol "$tmp_first" requires explicit package name at so.pl line 6.
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 6, near "$tmp_first)"
Global symbol "@second" requires explicit package name at so.pl line 8.
Global symbol "@second" requires explicit package name at so.pl line 10.
Global symbol "$tmp" requires explicit package name at so.pl line 11.
Global symbol "$tmp_second" requires explicit package name at so.pl line 11.
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 11, near "$tmp_second)"
Global symbol "@first" requires explicit package name at so.pl line 13.
Execution aborted due to compilation errors.

我不知道您要做什么,但这是具有正确语法的代码:

use warnings;
use strict;

sub do_smth (\@\@);  # predeclaration needed since the prototyped sub
                     # is called recursively
sub do_smth (\@\@) {
    my ($first, $second) = @_;
    my @tmp_first = @$first;
    my $tmp = shift(@tmp_first);
    if (@tmp_first > 0){
        do_smth(@tmp_first, @$second);
    }
    my @tmp_second = @$second;
    $tmp = shift(@tmp_second);
    if (@tmp_second > 0){
        do_smth(@$first, @tmp_second);
    }
}

This code does not even compile. Without warnings and strict you will get these errors:

Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 5, near "$tmp_first)"
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 10, near "$tmp_second)"
Execution aborted due to compilation errors.

and with warnings and strict:

Illegal character in prototype for main::do_smth : @first,@second at so.pl line 4.
Global symbol "@first" requires explicit package name at so.pl line 5.
Global symbol "$tmp" requires explicit package name at so.pl line 6.
Global symbol "$tmp_first" requires explicit package name at so.pl line 6.
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 6, near "$tmp_first)"
Global symbol "@second" requires explicit package name at so.pl line 8.
Global symbol "@second" requires explicit package name at so.pl line 10.
Global symbol "$tmp" requires explicit package name at so.pl line 11.
Global symbol "$tmp_second" requires explicit package name at so.pl line 11.
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 11, near "$tmp_second)"
Global symbol "@first" requires explicit package name at so.pl line 13.
Execution aborted due to compilation errors.

I dont know what you are trying to do, but here is your code with the proper syntax:

use warnings;
use strict;

sub do_smth (\@\@);  # predeclaration needed since the prototyped sub
                     # is called recursively
sub do_smth (\@\@) {
    my ($first, $second) = @_;
    my @tmp_first = @$first;
    my $tmp = shift(@tmp_first);
    if (@tmp_first > 0){
        do_smth(@tmp_first, @$second);
    }
    my @tmp_second = @$second;
    $tmp = shift(@tmp_second);
    if (@tmp_second > 0){
        do_smth(@$first, @tmp_second);
    }
}
独留℉清风醉 2024-10-01 12:30:56

您正在移动(未定义的)标量 $tmp_first 和 $tmp_second。

还没再看下去。

You are shifting the (undefined) scalars $tmp_first and $tmp_second.

Haven't looked any further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文