0

Please, help me to write correctly function on Erlang. Function should remove packages (which successfully sent) from the queue. I think it should be like that, but it does not work. Erlang says: illegal pattern on line 3 :(

delete_pkts(AcksList, State) ->
  NewSendingList = lists:filter(fun(X) ->
    lists:any(fun(E) -> case E of X#internal_packet.message_number -> false; _ -> true end end, AcksList)
  end, State#state.pkt_send),
  State#state{ pkt_send = NewSendingList }.

1 Answers1

1

I've never understood why, but you can't put a record field reference in a case clause. You could match out the field you're interested in in the function head instead:

delete_pkts(AcksList, State) ->
  NewSendingList = lists:filter(fun(#internal_packet{message_number = MsgNo}) ->
    lists:any(fun(E) -> case E of MsgNo -> false; _ -> true end end, AcksList)
  end, State#state.pkt_send),
  State#state{ pkt_send = NewSendingList }.

Alternatively, as the case expression just returns false if E matches, and true otherwise, you could use the =/= operator:

delete_pkts(AcksList, State) ->
  NewSendingList = lists:filter(fun(X) ->
    lists:any(fun(E) -> E =/= X#internal_packet.message_number end, AcksList)
  end, State#state.pkt_send),
  State#state{ pkt_send = NewSendingList }.
legoscia
  • 39,593
  • 22
  • 116
  • 167
  • 1
    Assuming for example message_number being the second field in record internal packet X#internal_packet.message_number is synonymous with element(3, X) and that is a function call which isn't allowed either in a clause – Peer Stritzinger Sep 26 '14 at 19:59