Loading ...
Sorry, an error occurred while loading the content.

List.mem and equality

Expand Messages
  • Boris Hollas
    Hello, I have tree-like data types for which a custom equality operator is defined. I want to test membership in lists, but I haven t found a way to supply
    Message 1 of 2 , Jun 14, 2012
    • 0 Attachment
      Hello,

      I have tree-like data types for which a custom equality operator is
      defined. I want to test membership in lists, but I haven't found a way
      to supply List.mem with a custom equality operator. So I may
      - write my own mem_with_predicate function or
      - use hashtables instead with a custom equality operator made with
      Hashtbl.Make.
      Both approaches are rather cumbersome, given that ADTs and lists are an
      integral part of the language. Opinions?
      --
      Best regards,
      Boris
    • Gabriel Scherer
      You can use List.exists: let my_mem elt = List.exists (my_eq elt) If you are planning to store and test membership on a lot of elements, you should use a more
      Message 2 of 2 , Jun 14, 2012
      • 0 Attachment
        You can use List.exists:

        let my_mem elt = List.exists (my_eq elt)

        If you are planning to store and test membership on a lot of elements,
        you should use a more appropriate data structure. If you can define a
        comparison operation (rather than equality), Set is a good choice,
        otherwise you'll have to use hashing + equality.

        It's a good idea to implement comparison directly if you can do so easily.

        On Thu, Jun 14, 2012 at 7:04 PM, Boris Hollas
        <hollas@...-dresden.de> wrote:
        > Hello,
        >
        > I have tree-like data types for which a custom equality operator is
        > defined. I want to test membership in lists, but I haven't found a way
        > to supply List.mem with a custom equality operator. So I may
        > - write my own mem_with_predicate function or
        > - use hashtables instead with a custom equality operator made with
        > Hashtbl.Make.
        > Both approaches are rather cumbersome, given that ADTs and lists are an
        > integral part of the language. Opinions?
        > --
        > Best regards,
        > Boris
        >
        >
        >
        > ------------------------------------
        >
        > Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
        > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
        > Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
        >
        >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.