I will assume that you want to get the elements of the original list that are at distance less than 2 from 2.
Objective Caml version 3.11.1
# let test x = abs (x - 2) <= 2 ;;
val test : int -> bool = <fun>
# List.filter test [4;1;7;3;8;9;2;0] ;;
- : int list = [4; 1; 3; 2; 0]
#
List.filter
is a function from the standard library. List.filter f l
produces the list of elements of l
for which f
answers true
.
Getting the function that decides if each element should go in the results list is orthogonal to the problem of filtering the list once you have this function, so you should do that first.
If you wish to use for f
a function that is the transitive closure of a relation that you have, you can use the library ocamlgraph to obtain that transitive closure. Specifically, of these functions, use add_vertex
for each puzzle piece, add_edge
for each relation that you have, and then apply function transitive_closure
to get a new graph g
in which you can ask if there is an edge between two elements e1
and e2
with mem_edge g e1 e2
. The partially applied function mem_edge g e1
can be passed to List.filter
.