7

As a beginner I understand that including cl-lib will allow me to use some code from Common Lisp. However, whenever I see this included in elisp code I wonder again, Should I really start with Common Lisp, then learn elisp? Is cl-lib giving elisp Common Lisp a common (or desirable) occurrence in best-practice elisp coding? I guess I'm looking also for "best practice" guidance here. But then "best practice" questions can be interpreted either way: as legitimate stackexchange-compliant question, or as a matter of opinion.

10
  • You can customize most aspects of Emacs without ever learning CL, but there are some advanced customization that require some basic understanding of a few of those functions. Without learning CL, I am two+ years into using Emacs and have customized everything I use on a daily basis-- email, calendar task/event, file management, document management, etc. Changing the colors and adding additional keyword categories to the lisp-mode.el took some very basic understanding. Fixing problems created by changes in the cl/eieio libraries in the Emacs Trunk also took some basic understanding.
    – lawlist
    Aug 28, 2015 at 2:11
  • If you are going to be writing your own complex libraries that have some really cool features that are beyond the level of mere mortals, then yes, learn CL. For example, the calfw library is super cool and has lots of that CL stuff.
    – lawlist
    Aug 28, 2015 at 2:14
  • 1
    Could you edit the post to make the question more discrete, please? Right now, it's very broad and likely to elicit opinion-based answers.
    – Dan
    Aug 28, 2015 at 2:17
  • 4
    You can learn Emacs Lisp first or Common Lisp first. The latter is much bigger, of course. The question of how you or anyone might want to proceed is far too broad for this site, and answers will likely be opinion-based. Such a question is better asked elsewhere. You can try [email protected] or reddit or other venues more amenable to discussion etc.
    – Drew
    Aug 28, 2015 at 6:28
  • 4
    The title certainly should not be followed by "The basic answer is yes." The basic answer is NO -- using cl-lib is NOT crucial for writing good Emacs Lisp code. Such a statement, and probably the question itself, is preposterous. Far too broad, opinion-related, and unconstructive for this site, IMHO.
    – Drew
    Aug 28, 2015 at 15:38

3 Answers 3

9

Emacs lisp is a programming language designed to be used to provide Emacs extensions and to program Emacs. Common Lisp is a programming language that was designed to be practical Lisp useful as production language. Therefore, Emacs Lisp is sometimes "a bit strange"(this may be an opinion), since you have to care about environment - Emacs - while Common Lisp is designed to be useful and powerful. This makes it have some useful features that Emacs Lisp does not have. However, since they are based on the same Lisp ancestor, they both have similar syntax, and can be tied together, as shown in cl.

If you don't know Common Lisp, don't learn it now(unless you want to, then go on). Learn Emacs Lisp first, as it's perfectly capable of doing everything you want(it just may be a little harder without cl in some cases). When you need more power, or learn Common Lisp, you can start incorporating it in your code.

Also note, that Common Lisp is just an another language, a tool. There is no "best practice", as nearly every programmer has his own definition of what "best practice" is. If you want to write good Emacs Lisp code, start reading some sources(e.g. Emacs source code files like simple.el or some packages, like smartparens), and try to follow some of these guidelines.

You can also read some style guides, although keep in mind that these aren't holy laws of coding style.

8

Emacs Lisp doesn't really have "Best Practices". Judging from all the code I've read so far, people instead go for what is most convenient for them. And given that Emacs Lisp and Common Lisp are reasonably close (both are of the Lisp-2 variety), it is no wonder either the older and unprefixed cl.el or the newer and prefixed cl-lib library are used when the built-in functions don't do it.

However this isn't the only route you can go. There's dash.el, a more list-oriented library lending both names and idioms from Clojure. I prefer using it these days as its list manipulation functions are richer and it comes with a bunch of goodies including -let for destructuring and -when-let for shortening the common (let ((foo ...)) (when foo ...)) idiom. Sometimes I add cl-lib to the mix, like when I need cl-loop for advanced loops or cl-letf for dynamically binding functions. Nothing wrong with mixing and matching these as you need or even inventing your library and using it.

6

No, it's definitely not crucial: A quick sed | sort | uniq -c says that within Emacs's own code we have:

 [...]
 20 (cl-ecase
 24 (cl-labels
 26 (cl-callf
 26 (cl-defmacro
 26 (cl-dolist
 26 (cl-remove-if
 31 (cl-flet
 43 (cl-return
 55 (cl-destructuring-bind
 57 (cl-defun
 58 (cl-decf
 75 (cl-letf
 91 (cl-check-type
120 (cl-case
123 (cl-pushnew
132 (cl-defstruct
306 (cl-assert
324 (cl-loop
386 (cl-incf

While these numbers are crude (many occurrences were missed), of all these uses, the only one that really matters is cl-defstruct: all others can be replaced with non-cl-lib code without losing much convenience or clarity.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.