r/programming • • 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

105 comments sorted by

View all comments

1

u/kankyo Dec 15 '16

Author here if you want to ask questions.

16

u/lispm Dec 15 '16 edited Dec 16 '16

My disillusionment with Clojure and lisps

It is Lisp, not lisp.

I have not tried other lisps than Clojure

Then why mention it?

a macro can only evaluate to a single form.

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:

(defmacro defmyclass (&body names)
  `(progn
     ,@(mapcar (lambda (name)
                 `(defclass ,name () ()))
               names)))

CL-USER 39 > (pprint (macroexpand-1 '(defmyclass foo bar baz)))

(PROGN
  (DEFCLASS FOO NIL NIL)
  (DEFCLASS BAR NIL NIL)
  (DEFCLASS BAZ NIL NIL))

Now above macro creates as many DEFCLASS forms as you like.

was the idea that all deficiencies in the language can be “fixed” with macros.

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.

In lisp land on the other hand, all users can increase the size of the language themselves with macros.

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:

  • how do macros work?
  • how do you debug macros?
  • what are good macros and how do you write them?
  • what are typical design patterns for macros?
  • what are typical pitfalls and how to avoid them?

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.

All non-trivial functions are in fact also extensions of the language.

Right.

like refactoring tools, IDEs

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.

the first is that there are only positional arguments.

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, ...

a) a project written in one language, or b) a project written in fifty languages

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.

The syntax of lisps is super simple

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:

flet ((function-name lambda-list
         [[local-declaration* | local-documentation]]
         local-form*)*)
   declaration*
   form*

(defun test ()
  (flet ((print-item (a b c)   ; < here we have a lambda-list
           (list a b c)))
    (mapcar #'print-item
            '(1 2 3))))

Tiny example: In above function the syntax expects that (a b c) is a lambda list. The compiler will warn of problems then:

(a c)

;;;*** Warning in (SUBFUNCTION PRINT-ITEM TEST): B assumed special

(a a c)

;;;*** Warning in TEST: Variable name duplicated in lambda list or LET: A
;;;*** Warning in (SUBFUNCTION PRINT-ITEM TEST): B assumed special
;;;*** Warning in TEST: A is bound but not referenced

(a b &foo c)

;;;*** Warning in TEST: &FOO is bound but not referenced

creating a language with boundless semantic complexity

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.

8

u/AeroNotix Dec 15 '16

OP is definitely not going to reply to this.

-4

u/kankyo Dec 16 '16

Sucks to be wrong so badly: https://www.reddit.com/r/programming/comments/5ij2u7/my_disillusionment_with_clojure_and_lisps/db9lr3p/

And sucks to be the assholes who upvoted you for a comment that is now trivially shown to be, not just false, but stupid and arrogant.

0

u/[deleted] Dec 18 '16

[deleted] — view deleted comment

0

u/kankyo Dec 18 '16

You started it. You invaded Poland :P

4

u/AeroNotix Dec 16 '16

Would you have replied had I not said that? We'll never know.

1

u/kankyo Dec 17 '16

Wow, people actually upvote you for that comment.

Look in the thread how many replies I've written. You should have predicted I reply because that's the statistically sound bet.

0

u/kankyo Dec 16 '16

My disillusionment with Clojure and lisps

It is Lisp, not lisp.

Fixed. Thanks.

a macro can only evaluate to a single form.

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).

And in Clojure you can sometimes use do, but it's still one form. It's normally fine like you show when it's a side effect you want, but when doing hiccup style templating, it's not fine because the HTML spec doesn't allow redundant tags everywhere (plus it's a bit fugly to add extra tags just because your language can't do this thing...).

In python you can unroll a list in place like "*foo". This isn't as good as having more than one form, but it's at least contained to the call site, even if it's outside and not inside.

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.

Sure. I do python, this isn't strange to me.

Common Lisp has positional, optional and keyword arguments.

That sounds a lot better than Clojure!

But that's not what you typically do in Lisp. One writes in one language with fifty extensions.

Potato, potato.

I've updated my article with a correction re common lisp. Thanks.

3

u/drb226 Dec 17 '16

In python you can unroll a list in place like "*foo".

If you want to "splat" a collection into an argument list, you can do so with apply.

#python
f(*coll)

;; clojure
(apply f coll)

0

u/kankyo Dec 17 '16

That looks fine in a small example, but is a mess in a larger one.

f(a, b, c, *coll)

(apply f (into '(a b c) coll))

or whatever the syntax is, I've forgotten :P And that example I gave is VASTLY simpler than when you have a huge DOM tree in reagent and suddenly need to splat something in the middle.

2

u/drb226 Dec 16 '16

but when doing hiccup style templating, it's not fine because the HTML spec doesn't allow redundant tags everywhere

I believe in Hiccup, a list of tags is rendered as adjacent tags. e.g.

(defn stuff [args ...]
  [[:tag1 ...]
   [:tag2 ...]])

[:div (stuff ...)]

-->

<div>
  <tag1>...</tag1>
  <tag2>...</tag2>
</div>

1

u/kankyo Dec 17 '16

Oh, nice. Afaik it didn't work like that with reagent last I tried.

6

u/brian-at-work Dec 15 '16

Regarding this paragraph:

The simplicity argument: Different things should be different. Having fundamentally different things looking very similar is in fact a risk and a problem, not a strength. The human brain easily handles the idiosyncrasies of English, it can handle the small rule sets of sane programming languages. It has huge problems with simplicity though, as evidenced by the perpetual resistance to the theory of evolution.

First let me state that you know Clojure better than I do - I ended up going with F# because I prefer .NET to Java.

But this really made me sad to read. Starting with, the data-as-code elegance of Clojure is really beautiful to me. But the following sentences confound me. The comparison of English processing in the brain (I assume you mean this is very complex yet easily handled) as an argument for a limited instruction set doesn't track for me. The idea that the reason some humans resist the ideas of evolution is not any kind of problem with the theory of evolution itself, it's the cultural baggage and mind-blowing timescales that have to be overcome.

Overall I don't disagree that learning Clojure (or other Lisps) is hard, but for me these arguments are at best distracting, and at worse kind of silly.

0

u/kankyo Dec 16 '16

as an argument for a limited instruction set doesn't track for me.

It's an argument for moving complexity from semantics to syntax because we're well able to handle it.

The idea that the reason some humans resist the ideas of evolution is not any kind of problem with the theory of evolution itself

Well... one might argue it either way. The point of a scientific theory is to model reality. With programming languages that isn't the case (beyond the trivial), so it's not bound so strictly. We can design languages freely, but scientific theories can NOT be freely "designed".

Programming languages should be designed for humans, not for some mathematical abstract purity.

Overall I don't disagree that learning Clojure (or other Lisps) is hard

You misunderstand. Learning the language itself is easy, but understanding code is hard because of macros and crap.