Hello, I think I have a podcasty question! I'm new to Clojure and wondering: is it useful to study Algebraic Data Types? do people use them in Clojure? How do or don't they fit in with the Clojure language and philosophy?
@yosevuk Welcome to the channel! I am of the opinion you can get quite a lot done without studying the theory of functional programming. Much of the published theory relates to type systems, and isn't as necessary in Clojure as it is in languages such as Haskell.
Personally, I find the theory very interesting, but I don't think it's very helpful for getting started in functional programming. Clojure allows you to be quite productive without having to learn a bunch of theory.
Clojure doesn't support Algebraic Data Types as a feature of the language because Clojure isn't statically typed (like Haskell). However, Clojure is strongly typed and it allows you to create new types using defrecord
and deftype
. You can then match on those types in a multimethod. Basically, you can have a similar style of programming as Haskell, but it isn't strictly necessary to approach things that way in Clojure.
For example, in the class tree ADT, instead of having an Empty
, Leaf
, and Node
type, you can use tuple like lists: [:empty]
, [:leaf value]
, [:node left-tree right-tree]
.
Then you can dispatch on the keyword: (defmulti tree-function first)
Ah, so as I understand, algebraic types aren't a feature, but the langauge does provide the tools to program in that style if you really want to. Thanks for that context @neumann! I have to read more about datatypes and multimethods.
@yosevuk That sounds like a great summary. You're welcome!