views:

393

answers:

3

Whenever in Haskell we need some variant data type, we would use ADTs in conjunction with pattern matching. What do Clojure folks use for such usecases?

+5  A: 

Well, there are actually some pattern matching libraries written for Clojure. Clojure's macros make this sort of thing possible. Matchure is one of the most recent. There are even some stuff for ADTs in contrib.

Disregarding that stuff, the closest thing we have to Haskell's ADTs in core Clojure is the new records and datatypes in Clojure 1.2. But, unless you need the benefits that come from using a record or datatype, you'll usually just use a map. Clojure is a dynamic language, so you wont get static type checking if you use a record and such anyway.

Clojure has "destructuring" which is reminiscent of pattern matching, and is used a lot in idiomatic Clojure. See this and this. The former is an answer by yours truly here on SO. ;p

Rayne
+1  A: 

Look to matchure library - it should be included in clojure-contrib in near time

Alex Ott
A: 

I like yap very much as a pattern matching library.

The fact to have both keywords and symbols makes implementing variant quite easy on top of a pattern matching library.

Nicolas Oury