这是我的代码:
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).
Run Code Online (Sandbox Code Playgroud)
我收到错误:
32> c(problem_7).
./problem_7.erl:30: call to local/imported function is_prime/1 is illegal in guard
Run Code Online (Sandbox Code Playgroud)
我不能在'if'表达式中使用局部函数:
if is_prime(Counter)->
problem_7(Counter, Primes + 1, Counter + 1);
true ->
problem_7(Current, Primes, Counter + 1)
end.
Run Code Online (Sandbox Code Playgroud)
我只是想知道是否有任何方法在守卫中使用本地功能,如何用本地函数写条件?
我是Erlang的新手,正在尝试编写一个有界缓冲问题程序.它几乎可以工作,除了确保生产者不会过早地覆盖未使用的数据.为了解决这个问题,我决定尝试在我的buffer()函数上设置保护程序,这样我就可以在缓冲区已满时使用一个没有接收的版本,在缓冲区为空时使用一个没有发送的版本,并且正常其余时间的版本.
我的问题是,无接收器版本的保护要求我知道代表缓冲区的数组的大小,这需要调用array:size/1.显然,Erlang不允许在警卫中进行函数调用,这会阻止它工作.有没有办法解决这个问题而不改变我的缓冲区actor的函数声明?
%% buffer: array num num
%% A process that holds the shared buffer for the producers and consumers
buffer(Buf, NextWrite, NextRead) when NextWrite == NextRead ->
io:format(" * ~w, ~w, ~w~n", [array:to_list(Buf), NextRead, NextWrite]),
receive
{enqueue, Reply_Pid, Num} ->
io:format("~w: > ~w~n", [Reply_Pid, Num]),
buffer(array:set(NextWrite rem array:size(Buf), Num, Buf), NextWrite + 1, NextRead);
finish ->
io:format("finished printing~n")
end;
buffer(Buf, NextWrite, NextRead) when (NextWrite - NextRead) == array:size(Buf) ->
io:format(" * ~w, ~w, ~w~n", [array:to_list(Buf), NextRead, NextWrite]), …Run Code Online (Sandbox Code Playgroud) 为什么这是非法的?
min1_e_( F, X, E) ->
if
F( X + 2*E ) < F( X + E ) -> % ?
min1_e_( F, X, E*2 );
true ->
E
end.
Run Code Online (Sandbox Code Playgroud)
我的意思是,如果我分别定义表达式的两个部分,它工作正常.但是比较函数返回应该是微不足道的,不应该吗?以为我错过了更多的东西.
在Erlang中编写if语句的最简单方法是什么,其中一部分守卫是member(E, L),即测试if是否E是列表的成员L?天真的方法是:
if
... andalso member(E,L) -> ...
end
Run Code Online (Sandbox Code Playgroud)
但是,因为我理解正确,member不是一个守卫表达是行不通的.哪种方式有效?
我正在尝试使用fermats方法创建一个素数因子.
该行生成错误
find_factors(A, B, FactorThis) when is_a_square(B) == true ->
Run Code Online (Sandbox Code Playgroud)
调用本地/导入函数is_a_square/1是非法的
我在这个实现中看到的唯一可能的替代方法是在函数中使用某种case语句.我正在避免这种情况,因为它可能搞砸尾部递归.我是Erlang noob. 还有哪些其他方法可以实现此功能?
get_int_part_of_sqrt(N) ->
trunc(math:sqrt(N)).
is_a_square(N) ->
get_int_part_of_sqrt(N) * get_int_part_of_sqrt(N) == N.
calculate_new_b(A, FactorThis) ->
NewB = trunc(abs((A * A) - FactorThis)),
io:format("Calculate_new_b A^2 ~w- FT ~w= NB ~w ~n",[A*A,FactorThis,NewB]),
find_factors(A, B, FactorThis) when is_a_square(B) == true ->
io:format("find_factors true ~w ~w~n", [A, B]),
{ok, A + get_int_part_of_sqrt(B), A - get_int_part_of_sqrt(B)};
find_factors(A, B, FactorThis) ->
io:format("find_factors false ~w ~w~n", [A, B]),
NewA = A + 1, …Run Code Online (Sandbox Code Playgroud) 下面的代码显示了我所询问的一个示例:为什么 Map、Enum 等...不能在保护子句或我自己的宏中使用。
defmodule GuardMod do
defmacro has_any_key(arg, keys) do
quote do
Map.keys(unquote(arg), unquote(keys)) |> Enum.any?
end
end
end
defmodule OtherMod do
import GuardMod
@doc """
has_any_key/2 is allowed, but Map.keys or Map.any not
"""
def fn1(list) when has_any_key(list, [:key1, :key2]), do: :nothing
end
Run Code Online (Sandbox Code Playgroud)