49 points · by dwighttk · 10y ago · 43 comments
Haven't messed with a LISP language since college but clojure has grown on me considerably. I already like it more than Scala... though I did almost immediately find myself reaching for a monad library (cats seems good so far) :) Haven't felt the need to write my own macros yet, but certainly many of the libraries I'm using (compojure) make heavy use of them.
Too bad my day job is all Java, all the time :( My continual requests to use Scala have been shot down over the last couple of years -- "We want something maintainable, where am I going to find Scala devs when you leave?" So obviously Clojure isn't going to fly :)
As an example, I've been using GraphQL quite heavily for the last 8 months, primarily with JavaScript and i've found it to be a fairly tedious experience. I tried out an Elixir GraphQL library that's heavily based on macros and was almost immediately more productive - despite not even knowing the language.
Similarly, coming up with a nice API for working with forms seems to be pretty much impossible with JavaScript. I've evaluated dozens, and not one comes close to the simplicity of Django's forms (which is only possible thanks to Python's metaclass magic).
Having sampled the power of languages with macros, I'm naturally drawn to Clojure, simply because it has a very decent JavaScript implementation. I'm sure there are other very good reasons for using the language, but right now macros are the motivation.
I'd like to understand why one would need 10 years of lisp experience before starting to author their own macros, it feels like a philosophy that's just going to stop people trying out lisp.
Also, given that Clojure is a lisp, I think you'll find you need macros left often than a language like elixir. They're still incredibly powerful, but you can do so much in Clojure without them that you would otherwise need a macro to do.
http://www.draketo.de/english/wisp#text-table-of-contents
[ed: For Racket, see also: https://github.com/takikawa/sweet-racket ]
[ed2: See also the "readable" package for Common Lisp and for Scheme: https://sourceforge.net/p/readable/wiki/Install-howto/ ]
Your macros will break all over the place in languages that don't have this property.
e.g. this is early in the stdlib and is what sets up the ability to write 'foo as a shorthand for (quote foo):
(extend-parser "'"
(mac (x) (list (quote quote) x)))
Haven't yet contorted myself into wanting to map a macro value over a list, though! ;; increment as a macro, anonymously
(= ((macro lambda (x) `(+ 1 ,x)) 10) 11)I have a diagnostics toggle (SIGUSR2) that traces what's happening with macros for debugging. Can you think of a better way to convey it? http://i.imgur.com/kFzEvd6.png
I imagine that you could implement a similar feature for tracing, and if you were careful writing the tracing code, you could then use that code for this feature, as you could say "trace the complication e.g. (trace (eval (read ...)))" and then maybe feed in an optional list of functions to elide . This would be super handy to have as a toggle'able feature, for both macros and normal user code. I use clojure.tools.tracing a lot, and it's a life saver.
Clojure doesn't allow user defined _reader macros_, which means it is less flexible than either CL or Scheme. Basically, you can't really define your own syntax parser in Clojure, you are limited to what the Clojure reader can already parse. In CL and Scheme you don't have this restriction.
Since then both R6RS and R7RS has support for unhygienic macros.
Note that there are two important properties of the macro systems of modern Scheme implementations: hygiene and referential transparency. The second property is often overlooked.
The standard syntax-rules macro system of R5RS Scheme is hygienic
If a macro transformer inserts a binding for an identifier (variable or keyword),
the identifier will in effect be renamed throughout
its scope to avoid conflicts with other identifiers.
and referentially transparent If a macro transformer inserts a free reference to
an identifier, the reference refers to the binding
that was visible where the transformer was specified,
regardless of any local bindings that may surround
the use of the macro.
The first property (automatically renaming of identifiers) is
prevents common errors. This property is easy to hack around in macro systems without automatic renaming. The macro writer simply generates a new identifier (gensym) each time a new identifier is needed.The second one referential transparency is the killer feature. Names inserted by a macro refers to binding where the macro is defined. This means that the macro writer has control over the meaning of the binding of identifiers in the expansion. In other words: a user of a macro can not by accident rebind or assign values to identifiers inserted by a macro use.
When referential transparency is not supported, then one must be careful to load libraries in the right order: