Note that there are some explanatory texts on larger screens.

plurals
  1. POUse of function in Guard not allowed. Suggestions for alternate implementation wanted
    text
    copied!<p>I am attempting to create a prime number factorizer, using <a href="http://en.wikipedia.org/wiki/Fermat%27s_factorization_metho" rel="nofollow noreferrer">fermats method</a>.</p> <p>This line generates an error</p> <pre><code>find_factors(A, B, FactorThis) when is_a_square(B) == true -&gt; </code></pre> <p><strong>call to local/imported function is_a_square/1 is illegal in guard</strong></p> <p>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. <strong>What other ways are there to implement this functionality?</strong></p> <pre><code>get_int_part_of_sqrt(N) -&gt; trunc(math:sqrt(N)). is_a_square(N) -&gt; get_int_part_of_sqrt(N) * get_int_part_of_sqrt(N) == N. calculate_new_b(A, FactorThis) -&gt; 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 -&gt; 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) -&gt; io:format("find_factors false ~w ~w~n", [A, B]), NewA = A + 1, NewB = calculate_new_b(NewA, FactorThis), find_factors(NewA, NewB, FactorThis). </code></pre> <p><a href="https://stackoverflow.com/questions/2241340/unable-to-use-function-call-in-function-guard">Research1</a> </p> <p><a href="https://stackoverflow.com/questions/6505213/is-there-a-way-to-use-local-function-in-guard">Research2</a> </p> <p>Edited. fixed argument in call to calculate_new_b</p> <p>added missing get_int_part_of_sqrts.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload