TCL因子计算代码:紧密括号后的额外字符 [英] TCL factorial calculation code: extra characters after close-brace

查看:1618
本文介绍了TCL因子计算代码:紧密括号后的额外字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是TCL中的代码,用于生成由用户作为参数给出的数字的阶乘。

  if {$ argc!= 1} {
put stderr错误!ns调用的参数数量错误! ($ argc)
exit 1
} else
set f [lindex $ argv 0]



proc Factorial {x} {
for {set result 1} {$ x> 1} {set x [expr $ x-1]} {
set result [expr $ result * $ x]
}
return $ result
}
set res [Factorial $ f]
puts$ f的factorial是$ res

有类似的 SO问题,但它似乎并没有直接解决我的问题。我已经双重检查代码的语法错误,但它不能在Cygwin中成功编译通过 tclsh 产生错误:

  $ tclsh ext1-1.tcl 
执行
时执行
后的额外字符如果{$ argc!= 1} {
put stderr错误! ns调用的参数数量错误! ($ argc)
exit 1
} else
set f [lindex $ argv 0]



proc Factorial {x} {...
(文件ext1-1.tcl第3行)

TCL代码:来自:NS Simulator for Beginners,Sophia-Antipolis,2003-2004

解决方案

Tcl对空格有点敏感比大多数语言(虽然不如Python那么多)。例如,除非在命令之间作为命令分隔符,否则不能添加未转义的换行符。另一组规则是:1)每个命令必须以与正确列表(其中元素由空格分隔)相同的方式写入;以及2)命令调用必须具有命令定义所指定的参数数目。



由于调用必须看起来像一个正确的列表,像

  ... {$ x> 1} {incr x -1} ... 

t work:以打开的大括号开头的列表元素必须以匹配的大括号结束,并且紧跟在与大括号匹配的大括号后面不能有任何文本。 (这听起来比它更复杂。)



参数数要求意味着

  for {set result 1} {$ x> 1} {incr x -1} {
set result [expr $ result * $ x]
}
命令需要四个参数(开始测试下一个正文),它只有两个,开始和mashup其余三个(其实甚至不是,因为mashup是非法的) 。



要使这项工作,参数需要分开:

  for {set result 1} {$ x> 1} {incr x -1} {
set result [expr {$ result * $ x}]
}

将空格(或制表符,如果需要)置于合法和正确的数字。


This is the code in TCL that is meant to produce factorial of a number given as parameter by the user.

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



proc Factorial {x}{
for {set result 1} {$x>1}{set x [expr $x - 1]}{
    set result [expr $result * $x]
}
return $result
}
set res [Factorial $f]
puts "Factorial of $f is $res"

There is a similar SO question, but it does not appear to directly address my problem. I have double-checked the code for syntax errors, but it does not compile successfully in Cygwin via tclsh producing the error:

$ tclsh ext1-1.tcl
extra characters after close-brace
    while executing
"if {$argc !=1}{
        puts stderr "Error! ns called with wrong number of arguments! ($argc)"
        exit 1
} else
        set f [lindex $argv 0]



proc Factorial {x}{..."
    (file "ext1-1.tcl" line 3)

TCL Code from: NS Simulator for Beginners, Sophia-Antipolis, 2003-2004

解决方案

Tcl is a little bit more sensitive about whitespace than most languages (though not as much as, say, Python). For instance, you can't add unescaped newlines except between commands as command separators. Another set of rules are that 1) every command must be written in the same manner as a proper list (where the elements are separated by whitespace) and 2) a command invocation must have exactly the number of arguments that the command definition has specified.

Since the invocation must look like a proper list, code like

... {$x>1}{incr x -1} ...

won't work: a list element that starts with an open brace must end with a matching close brace, and there can't be any text immediately following the close brace that matches the initial open brace. (This sounds more complicated than it is, really.)

The number-of-arguments requirement means that

for {set result 1} {$x>1}{incr x -1}{
    set result [expr $result * $x]
}

won't work because the for command expects four arguments (start test next body) and it's only getting two, start and a mashup of the rest of other three (and actually not even that, since the mashup is illegal).

To make this work, the arguments need to be separated:

for {set result 1} {$x>1} {incr x -1} {
    set result [expr {$result * $x}]
}

Putting in spaces (or tabs, if you want) makes the arguments legal and correct in number.

这篇关于TCL因子计算代码:紧密括号后的额外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆