From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id DAA20453; Mon, 26 May 2003 03:15:25 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id DAA20519 for ; Mon, 26 May 2003 03:15:24 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h4Q1FMH16782 for ; Mon, 26 May 2003 03:15:22 +0200 (MET DST) Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3p2/3.7W) with ESMTP id KAA00160; Mon, 26 May 2003 10:15:15 +0900 (JST) To: ll189417@zodiac.mimuw.edu.pl Cc: caml-list@inria.fr Subject: Re: [Caml-list] Hashtable, possible? In-Reply-To: References: <87znlbq2b6.dlv@wanadoo.fr> X-Mailer: Mew version 1.94.2 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20030526101514C.garrigue@kurims.kyoto-u.ac.jp> Date: Mon, 26 May 2003 10:15:14 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) X-Spam: no; 0.00; jacques:01 caml-list:01 hashtable:01 lukasz:01 lew:01 189417:01 zodiac:01 camlp:01 hashtables:01 bigarrays:01 belive:01 hash:01 reasonnable:01 notations:01 reuse:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Lukasz Lew >> Is it possible to implement with camlp4, syntax for using hashtables like >> arrays? [...] > So. If it is possibe, and there is such syntax for arrays, strings and > bigarrays, so why there isn't any for hashtables? > I belive hashtables are more often used than bigarrays... Maybe, but code using hash tables does not access/modify them several times on the same line... There is also a reasonnable concern about not having too many fancy notations in the language. Last, as ocaml does not use camlp4 by default, the answer to your question would not imply that this is possible in ocaml alone. However, if you are not using bigarrays, you can actually reuse their notation in the vanilla compiler, by just defining the following module: # module Bigarray = struct module Array1 = struct let set = Hashtbl.add let get = Hashtbl.find end end;; module Bigarray : sig module Array1 : sig val set : ('a, 'b) Hashtbl.t -> 'a -> 'b -> unit val get : ('a, 'b) Hashtbl.t -> 'a -> 'b end end # let f x y z = x.{y} <- z;; val f : ('a, 'b) Hashtbl.t -> 'a -> 'b -> unit = # let g x y = x.{y};; val g : ('a, 'b) Hashtbl.t -> 'a -> 'b = However you probably also want sugar for hash table creation, and this would indeed require camlp4: {|"a" => 1; "b" => 2|} But you can still do a lot with simple functions: # let (=>) x y = (x,y);; val ( => ) : 'a -> 'b -> 'a * 'b = # let hash l = let h = Hashtbl.create (max 7 (List.length l)) in List.iter (fun (k,d) -> h.{k} <- d) l; h;; val hash : ('a * 'b) list -> ('a, 'b) Hashtbl.t = # let h = hash["a"=>1;"b"=>2];; val h : (string, int) Hashtbl.t = # h.{"a"};; - : int = 1 --------------------------------------------------------------------------- Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp JG ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners