gpt4 book ai didi

Erlang 多个 Guards on `when`

转载 作者:行者123 更新时间:2023-12-04 22:46:25 26 4
gpt4 key购买 nike

其他stackoverflow-ers,

我目前正在学习 Erlang。
有人可以指出我为什么会收到 illegal guard expression用这个守卫?
add_new_prime(Idx, Primes, Ref) when length(Primes) =:= 0 ; math:sqrt(Idx) < hd(Primes) -> Ref ++ [Idx];
如果我只做 add_new_prime(Idx, Primes, Ref) when length(Primes) =:= 0 -> Ref ++ [Idx]; 来“取消优化”它

有用。我见过多个例子,其中每个子句有多个“声明”,但不明白为什么我的不起作用。

非常感谢!

最佳答案

请参见:

The set of valid guard expressions (sometimes called guard tests) is a subset of the set of valid Erlang expressions. The reason for restricting the set of valid expressions is that evaluation of a guard expression must be guaranteed to be free of side effects. Valid guard expressions are:

the atom true, other constants (terms and bound variables), all regarded as false, calls to the BIFs specified below, term comparisons, arithmetic expressions, boolean expressions, and short-circuit expressions (andalso/orelse).



在你的情况下, math:sqrt(Idx)不是有效的守卫表达式。

在此处阅读文档: http://www.erlang.org/doc/reference_manual/expressions.html#id81357

关于Erlang 多个 Guards on `when`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24254418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com