11

Can someone please explain what the specific issues are with using the cl package in elisp? As a new coder in emacs I feel as though I'm making a mistake whenever I reach for the (require 'cl) option. I have read and understood the byte-compilation issue with the cl package. I have looked at the old arguments and have no wish to revive them. I am also not looking for any generalist comment on whether common-lisp is better than x brand lisp.

What I would like to know is practically how to use common-lisp so that any elisp I write will have a good chance of being accepted by the majority of elisp coders. Specifically, should I avoid using common lisp entirely, or are there some parts of the language that are acceptable to everyone and some parts where a good majority of coders would snigger and scoff at?

Without wishing to limit the breadth of the answer, is this:

(mapcar (lambda(x) (* x x)) '(1 2 3))

much more acceptable than this:

(require 'cl)
(loop for el in '(1 2 3) collect (* el el))
2
  • 1
    It would be great to get some comment from anyone who doesn't like common-lisp (or maybe there isn't any and the myth is busted). Commented Oct 29, 2012 at 14:45
  • 3
    the CL package is not Common-Lisp, so you can like one and dislike the other. I like many parts of what the CL package offers, but there are other parts which are problematic for various reasons. The new cl-lib library in Emacs-24.3 cleans up the namespace so that we do not need to care if cl-lib is good or not. The part of CL I do not like have to do with features of Common-Lisp which are ill-suited to a "simple interpreter" implementation, so they can result in unexpected performance costs. (plus a few features whose implementation is flawed) Commented Oct 29, 2012 at 15:35

3 Answers 3

9

While using a lot of third-party libraries and writing some of my own eLisp code, I never encountered the situation when using CL package resulted in a name conflict. So, I'd be tempted to say that the argument against using CL is nothing but puritanism (in the original sense of the word, not meaning the religious side of things).

But, if you are planning on a very long time support and you want to have some sort of a backup, here's what I would do (but I'm not doing that myself, because fixing things once they are broken seems to be a better tactic). For the functions in CL package that you are using, create a special file, where you defalias all of them to have a cl- prefix. So, for example, instead of having a (position ...) you would have (cl-position ...). Theoretically will save you the problem of forward compatibility. However functions don't get removed instantly, you'll get a warning ahead of time of them being deprecated, and will have a lot of time to update. Up to you, really.

Loop macro in Common Lisp is a controversy all by itself, it is not a typical construct for the language and that's why, for example, the iterate library exists. It also requires that you learn "the loop mini-language" to use it well, which is sort of a small domain-specific language, and there's really no requirement that this kind of construct use one. BUT, loop has its strong sides. List processing functions such as mapcar or reduce will serve you well in more trivial cases, like the one you have in your example, but in the less trivial cases loop is going to be a better and also less verbose way of doing the same thing.

Sign up to request clarification or add additional context in comments.

Comments

4

Have you read the macros section of the manual? Using macros such as your loop example is perfectly okay.

But you would use eval-when-compile around the require.

In fact (eval-when-compile (require 'cl)) appears 66 times just in the root lisp folder in my Emacs.

2 Comments

Yes I have read the macro section, and have looked through some macro code in packages. I'm still waiting for that 'aha' moment when I find a problem that needs a macro solution.
No problem needs a macro solution. Quite a few can be simplified with macros. This is a clojure example rather than elisp, but I wrote a macro to extract all static fields of a java class in expose it within my own namespace. The 'solution' without macros would be to write the bindings one by one...
4

Unless you plan to integrate your code in Emacs use the CL package, and put this snippet in your .emacs to disable the warnings.

(byte-compile-disable-warning 'cl-functions)

Note: You can find some advices on elisp programming on nicferrier's blog.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.