* [Caml-list] Hashtable, possible?
@ 2003-05-24 19:52 Lukasz Lew
2003-05-25 0:34 ` Remi Vanicat
0 siblings, 1 reply; 6+ messages in thread
From: Lukasz Lew @ 2003-05-24 19:52 UTC (permalink / raw)
To: caml-list
Hi.
Is it possible to implement with camlp4, syntax for using hashtables like
arrays?
I think about something like this:
# let a = [{ }];;
val a : ('_a, '_b) Hashtbl.t = <abstr>
# a.{"foo"} <- 'f';;
- : unit = ()
I need only "yes" or "no" :)
Regards,
Lukasz Lew
-------------------
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
-------------------
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Hashtable, possible?
2003-05-24 19:52 [Caml-list] Hashtable, possible? Lukasz Lew
@ 2003-05-25 0:34 ` Remi Vanicat
2003-05-25 0:48 ` Remi Vanicat
0 siblings, 1 reply; 6+ messages in thread
From: Remi Vanicat @ 2003-05-25 0:34 UTC (permalink / raw)
To: caml-list
Lukasz Lew <ll189417@zodiac.mimuw.edu.pl> writes:
> Hi.
> Is it possible to implement with camlp4, syntax for using hashtables like
> arrays?
> I think about something like this:
>
> # let a = [{ }];;
> val a : ('_a, '_b) Hashtbl.t = <abstr>
>
> # a.{"foo"} <- 'f';;
> - : unit = ()
>
> I need only "yes" or "no" :)
yes it is
--
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Hashtable, possible?
2003-05-25 0:34 ` Remi Vanicat
@ 2003-05-25 0:48 ` Remi Vanicat
2003-05-25 13:40 ` Lukasz Lew
0 siblings, 1 reply; 6+ messages in thread
From: Remi Vanicat @ 2003-05-25 0:48 UTC (permalink / raw)
To: caml-list
Remi Vanicat <vanicat@labri.u-bordeaux.fr> writes:
> Lukasz Lew <ll189417@zodiac.mimuw.edu.pl> writes:
>
>> Hi.
>> Is it possible to implement with camlp4, syntax for using hashtables like
>> arrays?
>> I think about something like this:
>>
>> # let a = [{ }];;
>> val a : ('_a, '_b) Hashtbl.t = <abstr>
>>
>> # a.{"foo"} <- 'f';;
>> - : unit = ()
>>
>> I need only "yes" or "no" :)
>
> yes it is
well, you should use something else than [{ }] and a.{ ... } because
they already have a meaning, but if you find something then yes you
can.
--
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Hashtable, possible?
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
0 siblings, 2 replies; 6+ messages in thread
From: Lukasz Lew @ 2003-05-25 13:40 UTC (permalink / raw)
To: Remi Vanicat; +Cc: caml-list
On Sun, 25 May 2003, Remi Vanicat wrote:
> Remi Vanicat <vanicat@labri.u-bordeaux.fr> writes:
>
> > Lukasz Lew <ll189417@zodiac.mimuw.edu.pl> writes:
> >
> >> Hi.
> >> Is it possible to implement with camlp4, syntax for using hashtables like
> >> arrays?
> >> I think about something like this:
> >>
> >> # let a = [{ }];;
> >> val a : ('_a, '_b) Hashtbl.t = <abstr>
> >>
> >> # a.{"foo"} <- 'f';;
> >> - : unit = ()
> >>
> >> I need only "yes" or "no" :)
> >
> > yes it is
>
> well, you should use something else than [{ }] and a.{ ... } because
> they already have a meaning, but if you find something then yes you
> can.
thank you all.
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...
Lukasz Lew
-------------------
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Hashtable, possible?
2003-05-25 13:40 ` Lukasz Lew
@ 2003-05-25 13:50 ` Eray Ozkural
2003-05-26 1:15 ` Jacques Garrigue
1 sibling, 0 replies; 6+ messages in thread
From: Eray Ozkural @ 2003-05-25 13:50 UTC (permalink / raw)
To: Lukasz Lew, Remi Vanicat; +Cc: caml-list
On Sunday 25 May 2003 16:40, Lukasz Lew wrote:
> thank you all.
>
> 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...
Why don't you write one for yourself? It would also be good p4 practice.
Be careful not to do something that looks like something else in mathematics
though. That's how I would go about it.
Happy hacking,
--
Eray Ozkural (exa) <erayo@cs.bilkent.edu.tr>
Comp. Sci. Dept., Bilkent University, Ankara KDE Project: http://www.kde.org
www: http://www.cs.bilkent.edu.tr/~erayo Malfunction: http://mp3.com/ariza
GPG public key fingerprint: 360C 852F 88B0 A745 F31B EA0F 7C07 AE16 874D 539C
-------------------
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Hashtable, possible?
2003-05-25 13:40 ` Lukasz Lew
2003-05-25 13:50 ` Eray Ozkural
@ 2003-05-26 1:15 ` Jacques Garrigue
1 sibling, 0 replies; 6+ messages in thread
From: Jacques Garrigue @ 2003-05-26 1:15 UTC (permalink / raw)
To: ll189417; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2003-05-26 1:15 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-24 19:52 [Caml-list] Hashtable, possible? 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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox