0

I work with erlang

I want to make a function that will check if the Cin and Id is not null

I tried with:

if Cin /= null && Id/=null -> {ok,Cin et Id sont différents de null};
     true -> {nok,Cin et Id sont  null}

    end.

I know that the notion of '&&' does not exist in erlang

but I can not find the equivalent of this notion in erlang

4

3 回答 3

6

在 Erlang 中,使用andalso代替&&

if Cin /= null andalso Id/=null -> {ok,Cin et Id sont différents de null};

的使用andalso是短路的,等效于&&。正则and运算符总是计算表达式的两边并且不会短路。

于 2012-12-27T10:04:57.003 回答
6

通常,最好使用匹配:

case {Cin, Id} of
  {null, _} -> cin_null;
  {_, null} -> id_null;
  {_, _}    -> not_null
end

但也要注意,你可以完全不检查而侥幸逃脱。在函数头中添加一个守卫:

my_func(Cin, Id) when is_integer(Cin), is_binary(Id) ->
  do_something.

如果这不匹配,您就会崩溃,但这通常是您期望在代码库中发生的情况。

于 2012-12-27T13:51:03.740 回答
2

您可以创建函数并使用模式匹配:

is_null(null, null) ->
  true;
is_null(_, _) ->
  false.

在控制台中:

1> c(some_mod).
{ok,some_mod}
2> some_mod:is_null(null, 1).
false
3> some_mod:is_null(1, 1).   
false
4> some_mod:is_null(null, null).
true
于 2018-06-30T17:56:56.983 回答