Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang lists:filter/2 cannot use comma instead of andalso

Tags:

erlang

Following code gives me warning at compile time: Warning: use of operator '>' has no effect

rd(a,{x,y}),
List = [#a{x=1,y=2}, #a{x=3,y=4}],
lists:filter(
    fun(E) ->
        E#a.x > 1, E#a.y =:= 2
    end, List).

But when I substitute comma with andalso, there is no warning.

like image 459
goofansu Avatar asked Feb 20 '23 05:02

goofansu


1 Answers

Using comma in this case only separates two actions, without effect to each other: E#a.x > 1 and the next operation (which is result of function) E#a.y =:= 2

It means that in your case, filter function is equal to:

fun( E ) ->
   E#a.y =:= 2
end

Only if you're writing guard expressions usage of comma is equal to usage of andalso, otherwise - comma just a separator between actions.

So, you may rewrite your function in two ways:

1)

fun
(E) when E#a.x > 1, E#a.y =:= 2 ->
   true;
(_Othervise) ->
   false
end

2)

 fun( E ) ->
    (E#a.x > 1) andalso (E#a.y =:= 2)
 end
like image 193
stemm Avatar answered Mar 03 '23 02:03

stemm