I have just added a new library to clojure.contrib. It provides a
proof-of-concept implementation of algebraic data types. An example
for what you can do with it:
(deftype tree
empty-tree
(leaf value)
(node left-tree right-tree))
(def a-tree (node (leaf :a)
(node (leaf :b)
(leaf :c))))
(defn depth
[#^tree t]
(match t
empty-tree 0
(leaf n) 1
(node l r) (inc (max (depth l) (depth r)))))
(depth empty-tree)
(depth a-tree)
For more examples, see clojure.contrib.types.examples.
The reason why I call this a proof-of-concept implementation is that
it has a major drawback that seriously limits its applicability:
equality tests on objects created from these types are done by
identity, not by value equality, which makes them practically
useless. This is due to the fact that the objects are actually
implemented as closures. Another drawback, following as well from the
implementation as closures, is that the objects cannot have any meta-
data attached.
Closures are the simplest way to create a new Java class in Clojure,
but they have the disadvantage of equality-by-identity. I have an
idea for a better implementation, based on gen-class and proxy, but
before embarking on this I'd like to see some feedback on the current
implementation. Is this something you would like to use?
Konrad.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---