From: Chris Clearwater <chris@sharkzone.com>
To: caml-list@inria.fr
Subject: [Caml-list] Functor implementation
Date: Wed, 23 Jul 2003 06:56:44 -0500 [thread overview]
Message-ID: <20030723115644.GA28291@pyramid.twistedmatrix.com> (raw)
Hi all, I was curious as to how ocaml implements functors and finding no help
with google, I thought I could ask on the list. After giving it a little
thought I was thinking they might be implemented as follows:
type 'a tree = Empty | Node of ('a tree) * 'a * ('a tree)
type comparison = GT | EQ | LT
let node l v r = Node(l, v, r)
let single v = Node(Empty, v, Empty)
let rec insert c t a =
match t with
Empty -> single a
| Node(l, v, r) -> match (c a v) with
LT -> node (insert c l a) v r
| EQ -> t
| GT -> node l v (insert c r a)
let rec member c t a =
match t with
Empty -> false
| Node(l, v, r) -> match (c a v) with
LT -> member c l a
| EQ -> true
| GT -> member c r a
(* Functor implementation follows *)
type 'a setmodule = {
empty: 'a tree;
insert: 'a tree -> 'a -> 'a tree;
member: 'a tree -> 'a -> bool;
}
let make c = {
empty = Empty;
insert = insert c;
member = member c;
}
(* Use a functor *)
let compare x y = let cmp = x - y in if cmp > 0 then GT else if cmp < 0 then LT else EQ
let mymodule = make compare
let s = mymodule.insert mymodule.empty 5
EOF
How far off is Ocaml's way and is the performance comparable? And on a
sidenote, wouldn't it be beneficial for there to be a built in comparison
type and a function to convert from integers greater than less than or
equal to, to the comparrison type? I imagine this would clarify code
that uses comparisons greatly.
Thanks in advance for your answers!
-------------------
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
next reply other threads:[~2003-07-23 11:56 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-07-23 11:56 Chris Clearwater [this message]
2003-07-23 13:01 ` Xavier Leroy
2003-07-23 22:41 ` Chris Clearwater
2003-07-24 15:16 ` brogoff
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=20030723115644.GA28291@pyramid.twistedmatrix.com \
--to=chris@sharkzone.com \
--cc=caml-list@inria.fr \
/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