有没有办法使用本地功能来防范 [英] Is there a way to use local function in guard
问题描述
这是我的代码:
is_prime(Num)->
length(list_of_dividers(Num)) == 0.
problem_7(Current, Primes, Counter) when Primes >= 10001->
Current;
problem_7(Current, Primes, Counter) when is_prime(Counter) ->
problem_7(Counter, Primes + 1, Counter + 1);
problem_7(Current, Primes, Counter) ->
problem_7(Current, Primes, Counter).
我收到错误:
32> c(problem_7).
./problem_7.erl:30: call to local/imported function is_prime/1 is illegal in guard
我不能在'if'表达式中使用本地函数:
And i can not use local function in the 'if'-expression:
if is_prime(Counter)->
problem_7(Counter, Primes + 1, Counter + 1);
true ->
problem_7(Current, Primes, Counter + 1)
end.
我只是想知道是否有任何方法使用本地功能来保护,如何写条件使用本地函数?
I am just wondering if there is any way to use local function in guard and how can I write condition with local function?
推荐答案
限制一组有效表达式的原因是必须保证对保护表达式的评估是无副作用。 http://www.erlang.org/doc/reference_manual/expressions.html (第7.24节)
The reason for restricting the set of valid expressions is that evaluation of a guard expression must be guaranteed to be free of side effects. http://www.erlang.org/doc/reference_manual/expressions.html (section 7.24)
在函数中使用 case
语句。您应该可以在 和中使用本地功能,如果
case
。
Use case
statement inside your function. You should be able to use local function in and if
case
.
已编辑:同意@cthulahoops,如果 http://www.erlang.org/doc/reference_manual/expressions.html#id75927
Edited: Agree with @cthulahoops, I was wrong about if
http://www.erlang.org/doc/reference_manual/expressions.html#id75927
这篇关于有没有办法使用本地功能来防范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!