caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: ll189417@zodiac.mimuw.edu.pl
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Hashtable, possible?
Date: Mon, 26 May 2003 10:15:14 +0900	[thread overview]
Message-ID: <20030526101514C.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <Pine.LNX.4.44.0305251538270.22552-100000@zodiac.mimuw.edu.pl>

From: Lukasz Lew <ll189417@zodiac.mimuw.edu.pl>

>> 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 = <fun>
# let g x y = x.{y};;
val g : ('a, 'b) Hashtbl.t -> 'a -> 'b = <fun>

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 = <fun>
# 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 = <fun>
# let h = hash["a"=>1;"b"=>2];;
val h : (string, int) Hashtbl.t = <abstr>
# h.{"a"};;
- : int = 1

---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>

-------------------
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


      parent reply	other threads:[~2003-05-26  1:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-24 19:52 Lukasz Lew
2003-05-25  0:34 ` Remi Vanicat
2003-05-25  0:48   ` Remi Vanicat
2003-05-25 13:40     ` Lukasz Lew
2003-05-25 13:50       ` Eray Ozkural
2003-05-26  1:15       ` Jacques Garrigue [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030526101514C.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=ll189417@zodiac.mimuw.edu.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).