关于Erlang中未使用变量的警告

时间:2021-05-01 21:49:03

I recently started Erlang, and I notice I constantly get "Warning: variable X is unused" while compiling. For example, take the following function, which finds the maximum element in a list:

我最近启动了Erlang,我注意到编译时我经常收到“警告:变量X未使用”。例如,使用以下函数,该函数查找列表中的最大元素:

    max([Head|Tail]) ->
       max(Head,Tail).

    max(Element,[Head | Tail]) when Element < Head ->
       max(Head,Tail);
    max(Element,[Head | Tail]) ->
       max(Element, Tail);
    max(Element,[]) ->
       Element.

The compiler warns me that in the 3rd case of the function, Head is unused. How can the function be written without Head?

编译器警告我,在函数的第三种情况下,Head未使用。如何在没有Head的情况下编写函数?

3 个解决方案

#1


9  

This should suppress the warning without being confusing:

这应该抑制警告而不会混淆:

max(Element,[_Head | Tail]) ->
   max(Element, Tail);

#2


9  

If you name a variable _ instead of Name (e.g. _ instead of Head) the variable will not be bound, and you will not get a warning.

如果您将变量命名为_而不是Name(例如_而不是Head),则变量将不会被绑定,并且您将不会收到警告。

If you name a variable _Name instead of Name (e.g. _Head instead of Head) the variable will be bound, but you will still not get a warning. Referencing a variable beginning with _ in the code is considered very bad practice.

如果您将变量命名为_Name而不是Name(例如_Head而不是Head),则变量将被绑定,但您仍然不会收到警告。在代码中引用以_开头的变量被认为是非常糟糕的做法。

It is recommended to keep the name of the variable to improve the readability of the code (e.g. it is easier to guess what _Head was intended for than just _).

建议保留变量的名称以提高代码的可读性(例如,更容易猜出_Head的目的而不仅仅是_)。

#3


6  

    max([Head|Tail]) ->
       max(Head,Tail).

    max(Element,[Head | Tail]) when Element < Head ->
       max(Head,Tail);
    max(Element,[_| Tail]) ->
       max(Element, Tail);
    max(Element,[]) ->
       Element.

Should do the trick. The reason being that replacing 'Head' with '_' is syntax for saying that a parameter will be placed there, but I don't need it.

应该做的伎俩。将'Head'替换为'_'的原因是说参数将被放置在那里,但我不需要它。

#1


9  

This should suppress the warning without being confusing:

这应该抑制警告而不会混淆:

max(Element,[_Head | Tail]) ->
   max(Element, Tail);

#2


9  

If you name a variable _ instead of Name (e.g. _ instead of Head) the variable will not be bound, and you will not get a warning.

如果您将变量命名为_而不是Name(例如_而不是Head),则变量将不会被绑定,并且您将不会收到警告。

If you name a variable _Name instead of Name (e.g. _Head instead of Head) the variable will be bound, but you will still not get a warning. Referencing a variable beginning with _ in the code is considered very bad practice.

如果您将变量命名为_Name而不是Name(例如_Head而不是Head),则变量将被绑定,但您仍然不会收到警告。在代码中引用以_开头的变量被认为是非常糟糕的做法。

It is recommended to keep the name of the variable to improve the readability of the code (e.g. it is easier to guess what _Head was intended for than just _).

建议保留变量的名称以提高代码的可读性(例如,更容易猜出_Head的目的而不仅仅是_)。

#3


6  

    max([Head|Tail]) ->
       max(Head,Tail).

    max(Element,[Head | Tail]) when Element < Head ->
       max(Head,Tail);
    max(Element,[_| Tail]) ->
       max(Element, Tail);
    max(Element,[]) ->
       Element.

Should do the trick. The reason being that replacing 'Head' with '_' is syntax for saying that a parameter will be placed there, but I don't need it.

应该做的伎俩。将'Head'替换为'_'的原因是说参数将被放置在那里,但我不需要它。