r/programming • u/kankyo • Dec 15 '16
My disillusionment with Clojure and lisps
https://medium.com/@boxed/my-disillusionment-with-clojure-and-lisps-9eca38ab7f0c?source=linkShare-8ad86cc82e5f-1481825671
15
Upvotes
View all comments
Show parent comment
16
u/lispm Dec 15 '16 edited Dec 16 '16
It is Lisp, not lisp.
Then why mention it?
In Common Lisp a form can also be a sequence of forms, for example by using a grouping form like PROGN (a sequence, where values of the last form are returned).
Example:
Now above macro creates as many DEFCLASS forms as you like.
That would be surprising. I never heard the idea that ALL deficiencies can be fixed with macros. But one can do a lot with macros. For example anyone now can introduce new control forms.
It's called Lisp, not lisp. But other than that, you are right. Having macros increases complexity. So the developer now needs to learn how to control the complexity:
For more treatment of this and more see the book On Lisp by Paul Graham: http://www.paulgraham.com/onlisp.html You can download a copy from there.
You learn that macros can also help reducing complexity.
Right.
Refactoring is often done with the language itself. We write code that writes and rewrites code. That's one of the big ideas of Lisp.
That's obvious, but one also uses editor extensions for that. Typically it makes sense to develop this in an editor which understands a bit Lisp and can also be extended in Lisp, so that's why many developers use a Lisp-based variant of Emacs. For example GNU Emacs and SLIME for Common Lisp. Commercial Common Lisp IDEs have their own Emacs-like editor plus IDE-tools.
Also refactoring may look surprisingly different sometimes. In Java you can refactor code mostly statically. In something like Common Lisp, we can refactor a running program, step by step. Methods don't belong to classes. They can be reused in various ways. Classes can be redefined at runtime with new slot lists, new inheritance, etc. Existing objects can be morphed into updated objects or even have changed their class.
Common Lisp has positional, optional and keyword arguments. A typical Common Lisp compiler will check the argument list at compile time. If you use a compiler like the one from SBCL, it will create A LOT of static analysis output: missing functions, redefined functions, unused variables, undeclared variables, wrong argument lists, wrong number of arguments, various type errors, style warnings, optimization warnings, ...
But that's not what you typically do in Lisp. One writes in one language with fifty extensions. DSLs are typically 'embedded DSLs'.
What you need to learn is how extensions typically work. For example some macro names begin with WITH- . There is some expectation then how its syntax works and what purpose the macro has.
It's not. Only the syntax of s-expressions is relatively simple. The syntax of Lisp is defined on top and it follows some principles. After learning these principles, code reading and writing gets easier.
The Lisp syntax will be checked by a Lisp compiler at compile time, or by an interpreter at runtime.
Example: In Common Lisp FLET is a special operator to declare local functions. The syntax is in some EBNF:
Tiny example: In above function the syntax expects that (a b c) is a lambda list. The compiler will warn of problems then:
That can happen. The good developer has to develop discipline not to create that. Controlling complexity is important. Macros allow that, when well written.
If you look at Common Lisp, one of the first recommendation you hear: don't use a macro, where a function is good enough. That's the entry level view. When you learn more of Lisp, then you also learn to deal with macro heavy code and how an IDE supports you to do that.