If O(N) complexity is acceptable for your application, your code is perfect. For better complexity, you will need to solve the linear search problem, for example, by putting order on the elements and using binary search trees.
A related non-search issue replaces the list item with a known index:
val replaceAt : int -> 'a -> 'a list -> 'a list
There are more robust data structures for this problem than a standard list. Search for purely functional random access lists in the literature.
Interestingly, not a single language in the ML family (OCaml, F #, SML) defines replace or replaceAt in the standard list library. This probably means that users should redesign their code to avoid O(N) complexity of these operations.
t0yv0
source share