(********************************************************************)
(*  OCaml code from the book ``Learn Programming with OCaml''       *)
(*  See https://usr.lmf.cnrs.fr/lpo/                                *)
(*                                                                  *)
(*  Sylvain Conchon and Jean-Christophe Filliâtre                   *)
(*  Copyright 2025 Université Paris-Saclay and CNRS                 *)
(*                                                                  *)
(*  Openly licensed via CC BY SA 4.0                                *)
(*  See https://creativecommons.org/licenses/by-sa/4.0/deed.en      *)
(********************************************************************)

(* Program 49 on page 241
   Prefix Trees *)

module Make(L : Letter) : PersistentSet with type elt = L.t list =
struct

  module M = Map.Make(L)

  type elt = L.t list
  type t = { word : bool ; branches : t M.t; }

  let empty = { word = false; branches = M.empty }

  let is_empty t = not t.word && M.is_empty t.branches

  let rec mem x t =
    match x with
      |        [] ->
          t.word
      | i::l ->
          try mem l (M.find i t.branches) 
          with Not_found -> false

This document was generated using caml2html