计时器:带参数的 tc
给定以下模块:
run(N)-> timer:tc(?MODULE,fct,[N]).
我通过 run(100) 调用它。从 shell 中,我有这个:
{1, {'退出',{undef,[{解析器,循环,“d”}, {定时器,tc,3}, {erl_eval,do_apply,5}, {shell,表达式,7}, {shell,eval_exprs,7}, {shell,eval_loop,3}]}}}
100 被解释为字符 ($d = 100) 而不是整数! 我的错在哪里?
Given the following module:
run(N)->
timer:tc(?MODULE,fct,[N]).
I call it by run(100). from a shell and I have this:
{1,
{'EXIT',{undef,[{parser,loop,"d"},
{timer,tc,3},
{erl_eval,do_apply,5},
{shell,exprs,7},
{shell,eval_exprs,7},
{shell,eval_loop,3}]}}}
100 is interpreted as a char ($d = 100) and not as an integer !
Where is my fault ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Erlang 中,
[100]
和"d"
是无法区分的,上面显示的代码不是问题。 Erlang shell 很有帮助(对于某些帮助值)并将[100]
打印为"d"
因为它是一个仅包含表示可打印字符的整数的列表。真正的问题是上面的
undef
错误,我的猜测是您的解析器模块不包含您通过调用的函数
。parser:loop/1
解析器:fct/1In Erlang,
[100]
and"d"
are indistinguishable, the code you show above isn't the problem. The Erlang shell is being helpful (for certain values of help) and printing[100]
as"d"
because it's a list containing only integers representing printable characters.The real problem is the
undef
error in the above, my guess is that your parser module doesn't contain a functionparser:loop/1
that you call viaparser:fct/1
.您在编译时收到任何警告吗?我怀疑您至少会看到一条有关未使用功能的消息。在学习过程中,如果您看到警告消息,请对其进行调查、理解并纠正。一般来说,您希望您的代码没有警告消息。
如果以 MFA 风格调用函数,则必须在源代码中导出它。从您所显示的内容来看,尚不清楚它是否被命名为“fct”或“loop”。因此,请确保您的命名一致,并确保它已导出:您在源代码中需要这个(假设该函数称为“循环”并采用 1 个参数):
-export([loop/1])。
Erlang 中的错误消息一开始可能很难解读。花一些时间阅读更多内容并更加熟悉它们,您将为自己节省大量的时间。
Did you get any warnings on your compilation ? I suspect you will see at least one message about an unused function. As you are learning, if you see a warning message then investigate it, understand it and correct it. Generally speaking, you want your code to have no warning messages.
If a function is called in MFA style then it has to be exported in the source code. From what you've shown it's not clear if it is named "fct" or "loop". So, make sure your naming is consistent, and make sure it is exported : You need this in your source code (assuming the function is called "loop" and takes 1 argument) :
-export([loop/1]).
Error messages in Erlang can be tricky to decipher at first. Take some time to read more and become more familiar with them and you will save yourself lots of time going forward.