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.

MatthewRock
  • 1,453
  • 13
  • 27
147pm
  • 2,907
  • 1
  • 18
  • 39
  • 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 '15 at 02: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 '15 at 02: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 '15 at 02:17
  • @lawlist: I think you are answering my basic question, i.e., yes, you do want CL to do the "heavy lifting." Basic Emacs tweaking is done in elisp, but bigger stuff should either include CL or borrow from the CL mentality/best practices. – 147pm Aug 28 '15 at 02:57
  • 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 `help-gnu-emacs@gnu.org` or reddit or other venues more amenable to discussion etc. – Drew Aug 28 '15 at 06:28
  • Sorry, I thought that "common lisp features will be used" was a system message, likely to be a subject of future searches, but now I see it isn't (this was the reason why I didn't like the edit). – wvxvw Aug 28 '15 at 08:44
  • It hardly is, given the existence of [dash.el](https://github.com/magnars/dash.el). – wasamasa Aug 28 '15 at 10:30
  • I am wondering if the new title reflects what the OP had initially in mind by asking this question. – Name Aug 28 '15 at 12:08
  • 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 '15 at 15:38
  • It may be my bad - original topic was different and I only changed topic, not body. So as for now it may look silly, I'll change that. – MatthewRock Aug 30 '15 at 21:52

3 Answers3

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.

MatthewRock
  • 1,453
  • 13
  • 27
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.

wasamasa
  • 21,803
  • 1
  • 65
  • 97
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.

Stefan
  • 26,154
  • 3
  • 46
  • 84