请帮我在Erlang上正确编写函数。函数应从队列中删除包(成功发送)。我认为它应该是这样,但它不起作用。 Erlang说:第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 }.
答案 0 :(得分:1)
我从来没有理解为什么,但你不能在case子句中放入记录字段引用。您可以在功能头中匹配您感兴趣的字段:
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 }.
或者,因为如果case
匹配,false
表达式只会返回E
,否则会true
,您可以使用=/=
运算符:
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 }.