How to Search for an item in a List in Erlang?

后端 未结 7 1852
抹茶落季
抹茶落季 2021-01-04 07:56

I am writing a cache gen-server for the company Use. I am wondering how to search an item from the list as I want the cost of the search for comparing various data structure

7条回答
  •  南笙
    南笙 (楼主)
    2021-01-04 08:30

    Just to simplify on the example at https://stackoverflow.com/a/15587565/56250:

    listFind(Element, List) ->
      lists:member(Element, List).
    

    lists:member works with empty lists. Quick look at the source (https://github.com/erlang/otp/blob/07b8f441ca711f9812fad9e9115bab3c3aa92f79/erts/emulator/beam/erl_bif_lists.c#L184) suggests it executes lazily.

提交回复
热议问题