Basic use of Janestreet's List.Assoc find function

Hi there, I am working my way through Janestreet’s implementation of the List.Assoc module, trying to use the find function in an assocciation like this one:

let assoc = [(“one”, 1); (“two”, 2); (“three”, 3)]

but when I try the pattern matching:

match List.Assoc.find assoc “Two” with
|None -> 0
|Some x -> x

I get the following error:

Error: This pattern matches values of type 'a option
but a pattern was expected which matches values of type
equal:(string -> string -> bool) -> int option

Any hints on what I may be doing wrong?



List.Assoc.find works by checking for equality on the first component of each tuple (in this case, string values). Since Jane Street libraries avoid use of built-in equality, you need to explicitly pass the equality function you want it to use:

val find :
  ('a, 'b) Base.List.Assoc.t -> equal:('a -> 'a -> bool) -> 'a -> 'b option

Something like List.Assoc.find ~equal:String.equal should work.

Also note that your example is broken because "Two" is capitalized in the search but not the association list.

Thank you! Worked like a charm.

1 Like