are_the_same(A, A) ->
true;
are_the_same(_, _) ->
false.
在第一个子句中,两个参数都命名为A,这将导致它们相互进行模式匹配。或者确切地说,第一个参数将使用=运算符绑定到A变量,而第二个参数将使用=运算符绑定到A变量。但是由于A已经被绑定,它将被视为“比较”。你可以在文档中阅读更多关于这方面的信息。 当然,您也可以使用如下形式的guard来编写write first clouse:
You don't need the function string:equal/2 to compare strings; you can use the operators == or =:= , which are allowed in guard tests. For example:
foo(A, B) when A =:= B ->
equal;
foo(_, _) ->
not_equal.
Though in most cases you'd want to use pattern matching instead, as described in the other answer . NB: As of Erlang/OTP 20.0, string:equal(A, B) is no longer equivalent to A =:= B . string:equal/2 now operates on grapheme clusters, and there are also string:equal/3 and string:equal/4 that can optionally ignore case when comparing and do Unicode normalisation. So you need to understand what you mean by "equal" before settling on a comparison method.
The functions you can use in guards are limited because of the nature of Erlang's scheduling; specifically, Erlang aims to avoid side-effects in guard statements (e.g., calling to another process) because guards are evaluated by the scheduler and do not count against reductions. This is why string:equal does not work. That being said, you can use Erlang's pattern matching to match strings. Please bear in mind the use of strings as lists, binaries, or iolists (nested lists/binaries) in Erlang, and make sure you're testing/passing strings of the right type (iolists are particularly hard to pattern match and are usually best handled with the re module, or converting them to binaries via iolist_to_binary ). For example, say we want a function that tests to see if a string begins with "foo":
3条答案
按热度按时间cedebl8k1#
您可以像这样使用模式匹配:
在第一个子句中,两个参数都命名为
A
,这将导致它们相互进行模式匹配。或者确切地说,第一个参数将使用=
运算符绑定到A
变量,而第二个参数将使用=
运算符绑定到A
变量。但是由于A
已经被绑定,它将被视为“比较”。你可以在文档中阅读更多关于这方面的信息。当然,您也可以使用如下形式的guard来编写write first clouse:
omhiaaxx2#
You don't need the function
string:equal/2
to compare strings; you can use the operators==
or=:=
, which are allowed in guard tests. For example:Though in most cases you'd want to use pattern matching instead, as described in the other answer .
NB: As of Erlang/OTP 20.0,
string:equal(A, B)
is no longer equivalent toA =:= B
.string:equal/2
now operates on grapheme clusters, and there are alsostring:equal/3
andstring:equal/4
that can optionally ignore case when comparing and do Unicode normalisation. So you need to understand what you mean by "equal" before settling on a comparison method.vqlkdk9b3#
The functions you can use in guards are limited because of the nature of Erlang's scheduling; specifically, Erlang aims to avoid side-effects in guard statements (e.g., calling to another process) because guards are evaluated by the scheduler and do not count against reductions. This is why
string:equal
does not work.That being said, you can use Erlang's pattern matching to match strings. Please bear in mind the use of strings as lists, binaries, or iolists (nested lists/binaries) in Erlang, and make sure you're testing/passing strings of the right type (iolists are particularly hard to pattern match and are usually best handled with the
re
module, or converting them to binaries viaiolist_to_binary
).For example, say we want a function that tests to see if a string begins with "foo":
If you just want to test for a particular string, it's even easier: