Erlang several guardsmen on `when`

Stackoverflow -ers elements,

I am currently studying Erlang. Can someone point me to why I get illegal guard expressionwith this guard?

add_new_prime(Idx, Primes, Ref) when length(Primes) =:= 0 ; math:sqrt(Idx) < hd(Primes) -> Ref ++ [Idx];

If I "don’t optimize" it, doing only add_new_prime(Idx, Primes, Ref) when length(Primes) =:= 0 -> Ref ++ [Idx];

it works. I saw several examples in which there are several expressions, but it is not clear why my work does not work.

Thank you so much!

+4
source share
2 answers

Please look:

( ) - . , . :

, ( ), , BIF, , , , (andalso/orelse).

math:sqrt(Idx) .

: http://www.erlang.org/doc/reference_manual/expressions.html#id81357

+7

@BlackMamba, :

add_new_prime(Idx, [], Ref) -> Ref ++ [Idx];
add_new_prime(Idx, [Prime | Primes], Ref) when Idx < Prime * Prime -> Ref ++ [Idx];
...

/1 , . .

+3

Source: https://habr.com/ru/post/1544832/


All Articles