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