这是我的代码:
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'表达式中使用局部函数:
if is_prime(Counter)->
problem_7(Counter, Primes + 1, Counter + 1);
true ->
problem_7(Current, Primes, Counter + 1)
end.
我只是想知道是否有任何方法可以在守卫中使用局部函数,我该如何用局部函数编写条件?
最佳答案
限制有效表达式集的原因是,必须确保对保护表达式的求值没有副作用。 http://www.erlang.org/doc/reference_manual/expressions.html(第7.24节)
在函数中使用case
语句。您应该能够在if
和case
中使用局部函数。
编辑:同意@cthulahoops,关于if
http://www.erlang.org/doc/reference_manual/expressions.html#id75927我错了
关于erlang - 有没有办法在 guard 队中使用本地功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6505213/