List Library find function usage

I want to use the find function to find an address from a list of addresses
find(p : 'a => bool, l : list('a)) : option('a)

The document says that the ‘find’ function finds the first element of l fulfilling predicate p as Some or None if no such element exists.

How do I put the predicate?

For example, I have a list of addresses in state called ‘addresses’ and I want to see if an address, ‘addr’ exists in the list. What do I write?

  entrypoint addr_in_list1(the_addr : address, list : list(address)) : bool =
    List.find((a) => a == the_addr, list) == Some(the_addr)

would do this using List.find - I’m not sure why there isn’t a List.member, but you can always define it yourself:

  entrypoint addr_in_list2(the_addr : address, list : list(address)) : bool =
    member(the_addr, list)

  function
    member : ('a, list('a)) => bool
    member(_, []) = false
    member(a, a1 :: as) =
      if(a == a1) true
      else        member(a, as)
3 Likes