开发者

Use of function in Guard not allowed. Suggestions for alternate implementation wanted

开发者 https://www.devze.com 2023-04-05 18:21 出处:网络
I am attempting to create a prime number factorizer, using fermats method. This line generates an error

I am attempting to create a prime number factorizer, using fermats method.

This line generates an error

find_factors(A, B, FactorThis) when is_a_square(B) == true ->

call to local/imported fun开发者_StackOverflow中文版ction is_a_square/1 is illegal in guard

The only possible alternative I see to this implementation is to use some sort of case statement within the function. I was avoiding that, as it might screw up the tail recursion. I am a Erlang noob. What other ways are there to implement this functionality?

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,
    NewB = calculate_new_b(NewA, FactorThis),
    find_factors(NewA, NewB, FactorThis).

Research1

Research2

Edited. fixed argument in call to calculate_new_b

added missing get_int_part_of_sqrts.


Erlang deliberately restricts which functions you're allowed to call in guards. Here's a fairly recent discussion of the justification for this, its merits and drawbacks.

The only way around it is to use case. You can pretty easily rewrite this code to use case:

find_factors(A, B, FactorThis) ->
    case is_a_square(B) of
        true -> io:format("      find_factors true ~w ~w~n", [A, B]),
                {ok, A + B, A - B};

        false-> io:format("      find_factors false ~w ~w~n", [A, B]),
                NewA = A + 1,
                NewB = calculate_new_b(NewA, FactorThis),
                find_factors(NewA, NewB, FactorThis).

Note that the above code is still properly tail-recursive.

(I modified your code a little to take out the parts that I'm guessing you meant not to have there)


Here is another way to refactor around the issue.

Add the desired guard function as an argument at the caller. This turns it from a function with possible side effects, into true or false, which have no side effects. Then straight pattern matching will do the job.

main() ->
    List2 = find_factors_2 (10, 5, 105, is_a_square(5)),
    io:format("method 2 ~w~n", [List2]).

find_factors_2(A, B, _FactorThis, true) ->
    Offset = get_int_part_of_sqrt(B),
    {A + Offset, A - Offset};

find_factors_2(A, _B, FactorThis, false) ->
    NewA = A + 1,
    NewB = calculate_new_b(NewA, FactorThis),
    find_factors_2(NewA, NewB, FactorThis, is_a_square(NewB)).
0

精彩评论

暂无评论...
验证码 换一张
取 消