Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion 递归Tcl过程不返回任何内容_Recursion_Tcl - Fatal编程技术网

Recursion 递归Tcl过程不返回任何内容

Recursion 递归Tcl过程不返回任何内容,recursion,tcl,Recursion,Tcl,下面的代码应该返回给定给 使用递归过程将Tcl脚本作为输入参数 if {$argc !=1} { puts stderr "Error! ns called with wrong number of arguments! ($argc)" exit 1 } else { set f [lindex $argv 0] } proc Fact {x} { if { $x==1 } { return 1

下面的代码应该返回给定给 使用递归过程将Tcl脚本作为输入参数

if {$argc !=1} {
        puts stderr "Error! ns called with wrong number of arguments! ($argc)"
        exit 1
} else {
        set f [lindex $argv 0]
}


proc Fact {x} {
        if { $x==1 } {
                return 1
        }
        return  $x * Fact ($x-1)
}
set res [Fact $f]
puts "Factorial of $f is $res"
但是,它没有如下所示的输出:
$tclsh fact.tcl 5
5的阶乘是


我还尝试了
return[expr$x*Fact($x-1)]
,但它导致了一个解析错误。我不知道Tcl中的递归调用是否有什么我没有注意到的地方

如果使用名称空间,事情会变得更整洁:那么您的过程可以在
expr

% proc ::tcl::mathfunc::fact {n} {
    expr {$n == 1 ? 1 : $n*fact($n-1)}
}
% fact 3
invalid command name "fact"
% expr {fact(3)}
6
% expr {fact(6)}
720

试试
return[expr{$x*[Fact[expr{$x-1}]}]
。我稍后会解释为什么…@JohannesKuhn,
return[expr{$x*[Fact[expr{$x-1}]]}
有效,但这是一种语法,有这么多括号。Tcl不是从algol语法派生出来的语言。它看起来不像其他语言。这个怎么样:
proc-func{name args body}{proc::tcl::mathfunc::$name$args[list expr$body]};func事实n{$n==1?1:$n*fact($n-1)}