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

13072Re: "ocaml_beginners"::[] I need help deciphering Martin's hash table code

Expand Messages
  • Toby Kelsey
    Jan 2, 2012
    • 0 Attachment
      On 02/01/12 01:47, Jean Saint-Remy wrote:
      > type ('a, 'b) t = ('a, 'b list ref) Hashtbl.t
      >
      > let add tbl key data =
      > let r =
      > try Hashtbl.find tbl key
      > with Not_found ->
      > let r = ref [] in
      > Hashtbl.add tbl key r;
      > r in
      > r := data :: !r

      This is what it looks like with clearer names and indentation, hopefully you can
      now see how this works.

      let add tbl key data =
      let val_list =
      try
      Hashtbl.find tbl key
      with Not_found ->
      let new_val_list = ref [] in
      (Hashtbl.add tbl key new_val_list; new_val_list)
      in
      val_list := data :: !val_list


      Toby
    • Show all 3 messages in this topic