ClojureScript Macros: A (Not So) Long Journey [Part II]

ClojureScript Macros: A (Not So) Long Journey [Part II] hackernoon.com1 year ago in#Dev Love19

@frozarFabien ROZAR Clojurist at heart The writing of Clojure/Script macro may seem as a wizard senior programming craft, but this article will show you that it’s not the case. This is the second part of my journey to learn Clojure/Script macro and this article will deal with Clojure macro. Motivation (defn- add-bubble [appstate bubble] (update appstate :bubbles conj bubble)) (defn add-bubble! [bubble] (swap! appstate (fn [appstate_arg] (add-bubble appstate_arg bubble)))) I would like to write a macro, let say BANG , which will take the add-bubble function as argument and generate the add-bubble! function which is the side-effect version of it. Even if my goal is to write ClojureScript macro, I like to go one step after another, thus I thought it is a good idea to learn how to write Clojure macro first. ClojureScript takes its origin from Clojure, and from my experience in programming, it’s easier to build from solid basics rather than eagerly jump to the last step. Also from my experience, you usually get more documentation/ressources from the root language than its derivative. Macro: the origin During my try/error process in learning macro, I was wondering: where does this idea of macro came from? As Clojure is known to be a dialect of Lisp, the path to its origin is clear. Lisp was invented by John McCarthy and its team in the late 1950s at MIT. At this time, FORTRAN was really the main programming language used on the early years of IBM machines. When McCarthy discovered the FORTRAN language, he was fascinated by the idea of writing programs with “algebraic” means. But as his main topic was Artificial Intelligence, he was also convinced of the need of a different way to express a program to a computer. Another programming language which would allow to handle symbolic expressions. These thought lead him to create the LISP language. If you’re interested by the history of LISP language, I recommend you this article of Herbert Stoyan where you’ll get the historical context of McCarthy’s work as well as the atmosphere at this time between the machine builder, the compiler builder and R&D interests (read funding). From this article: Already in 1956 it was clear that one had to work with symbolic expressions to reach the goal of artificial intelligence. As the researchers already understood numerical computation would not have much importance. McCarthy, in his aim to express simple, short description parts by short language elements, saw the composition of algebraic sub-expressions as an ideal way to reach this goal. In this related article still of Herbert Stoyan, you can find a list of 24 new ideas for programming language (at this time) from McCarthy. I quote here some idea which are the essence of macro in my humble opinion: (4) extensibility of programs (incremental compiler) and changeability of programs,(10) possibilities for manipulating symbolic quantities. Rich Hickey, the author and maintainer of Clojure, has recently published an article titled “A History of Clojure”. This article explained his motivations around Clojure, some design choice he has made in the language, and obviously discuss about similarities and differences between LISP and Clojure. On the topic of macro, Clojure macros are similar to Common Lisp macros. One difference discussed is about how symbol are manipulated by these two languages.  » Read More

Like to keep reading?

This article first appeared on If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply