Skip to main content
34 votes
Accepted

How to concatenate two lists?

You can also just use append. (append '("a" "b" "c") '("d" "e" "f"))
John Kitchin's user avatar
  • 11.9k
22 votes
Accepted

Is '(a . b) really a list?

It satisfies listp, so in that sense it is a list. listp just tests whether something is a cons or nil (aka ()), on the one hand, or something else, on the other hand. A proper list or true list (or ...
Drew's user avatar
  • 78.1k
17 votes
Accepted

Removing the last element of a list

Yes there is: (setq list (butlast list)) That is a function from subr.el. (Loaded by default. No need to load anything.) You can also cut a tail with N elements by (setq list (butlast list N)) A ...
Tobias's user avatar
  • 33.4k
15 votes

Meaning of period in (. 123)

It looks like Emacs simply reads (. 123) as 123, what happened? That's exactly what happened. To back it up with sources: if (ch == '.') { if (!NILP (tail)) XSETCDR (tail, read0 (...
wasamasa's user avatar
  • 22.2k
15 votes
Accepted

add-to-list vs add-hook?

The question you asked The biggest difference is the last argument: local for add-hook and compare-fn for add-to-list. This means that you have no control over how add-hook decides whether it ...
sds's user avatar
  • 6,134
12 votes
Accepted

Convert 2-item list to a dotted pair?

cons makes a cons cell out of two arguments. apply uses a list as arguments to a function. When their powers combine: (apply #'cons x) converts a two element list into a cons cell (without having to ...
erikstokes's user avatar
  • 12.9k
11 votes
Accepted

Melpa - 'Failed to download melpa'

The error seems to be saying that ((\, "http://melpa.org/packages/")) doesn't match the regex "\`https?:" Let's look at that value, the one being added to package-archives: '("melpa", "http://melpa....
zck's user avatar
  • 9,112
9 votes
Accepted

How to remove / delete nth element of a list

Well, here's a destructive version I'd be happy with: (defun remove-nth-element (nth list) (if (zerop nth) (cdr list) (let ((last (nthcdr (1- nth) list))) (setcdr last (cddr last)) ...
wvxvw's user avatar
  • 11.4k
9 votes
Accepted

Are the graphical depictions in docs drawed in manual labour?

Included with emacs is a basic tool called picture-mode: To edit a picture made out of text characters (for example, a picture of the division of a register into fields, as a comment in a program)...
JeanPierre's user avatar
  • 7,475
9 votes
Accepted

Filter a list of strings to create a separate list of those that match a given prefix

(seq-filter (apply-partially #'string-prefix-p "a") '("apple" "pear" "grape" "apricot")) If you have other similar questions, consult the ...
shynur's user avatar
  • 5,578
8 votes
Accepted

Org Mode - Is there a way to reverse org-list-make-subtree?

The command org-ctrl-c-minus (bound to C-c - by default) turns a region of lines into a list. You need to mark the lines you want to change first, but it works with the headlines in your example as ...
glucas's user avatar
  • 20.7k
8 votes
Accepted

Shortening trees list with (setcdr (nthcdr 2 trees) nil)

Formal answer In your expanded example you set the variable trees to a new value in: (setq trees '(pine birch)) That is not what happens in the original example. In the original example really the ...
Tobias's user avatar
  • 33.4k
8 votes

When is the first element in the argument list regarded as a function symbol and when not?

the first element of a list is interpreted as a function symbol That's the most common case, but it isn't an absolute rule. First, you need to know that every symbol can have both a function meaning (...
Gilles 'SO- stop being evil''s user avatar
7 votes

How to check if a given string is a substring of an element of a list

The easiest way is to use the Common Lisp compatibility layer: (require 'cl-seq) (cl-member "ap" '("foo" "apa" "bar") :test #'string-match) ==> ("apa" "bar") PS. This is not directly relevant to ...
sds's user avatar
  • 6,134
7 votes

Report duplicates in a list?

Using dash: (defun find-duplicates (list) "Return a list that contains each element from LIST that occurs more than once." (--> list (-group-by #'identity it) (-filter (lambda (...
zck's user avatar
  • 9,112
7 votes

Adding many items to a list

There are many different ways. Not sure if there's a particularly "proper" way. There are considerations you should take depending on how you want to add it (destructively, non-destructively) or if ...
Piglet's user avatar
  • 156
7 votes
Accepted

How to check if two lists share any elements?

You can use seq-intersection, it is documented in its docstring and (elisp) Sequence Functions: seq-intersection is a compiled Lisp function in `seq.el'. (seq-intersection SEQUENCE1 SEQUENCE2 &...
xuchunyang's user avatar
  • 14.6k
7 votes
Accepted

How to subtract 1 from the nth element of a list of numbers?

Using the built-in cl-lib package, you can write the following: (eval-when-compile (require 'cl-lib)) (cl-decf (nth 1 x)) Alternatively, in vanilla Elisp: (setf (nth 1 x) (1- (nth 1 x))) Be ...
serycjon's user avatar
  • 381
6 votes
Accepted

Why does the member function return a list's tail rather than t?

This is a general Lisp question - a question about Lisp lists. It is not special to Emacs Lisp. The answers you are getting are all correct, and they say the same thing, so far. You apparently don't ...
Drew's user avatar
  • 78.1k
6 votes

add-to-list vs add-hook?

In some cases you can use either function to add an element to a list. That you can do that does not mean that you should, however. The recommendation by Emacs is to use only add-hook for a hook. ...
Drew's user avatar
  • 78.1k
6 votes
Accepted

Report duplicates in a list?

I think the easiest way is to use hash tables: (defun get-duplicates (list &optional test) (let ((ht (make-hash-table :test (or test #'equal))) ret) (dolist (x list) (incf (...
sds's user avatar
  • 6,134
6 votes
Accepted

How to sort an association list (alist)?

At the excellent ergo emacs on hash tables (http://ergoemacs.org/emacs/elisp_hash_table.html) I found out this also works for alists: (sort my-alist (lambda (a b) (string< (car a) (car b)))) ......
Erik's user avatar
  • 153
6 votes
Accepted

How to mapcar uneven lists?

The two built-in "zip-with" functions: seq-mapn from seq.el (Emacs ≥ 25.1) cl-mapcar from cl-lib.el (Emacs ≥ 24.3; was mapcar* in cl.el before that) and the most prominent third-party one: -...
Basil's user avatar
  • 12.5k
6 votes
Accepted

How to delete all list elements matching a regexp?

Do you want a new list that has only elements "b1" and "b2"? Or do you want the same list structure, but modified to have removed elements "a1" and "a2"? @rpluim shows one way to do the former. A ...
Drew's user avatar
  • 78.1k
6 votes

How can i add same value to every item in list, turning it into alist?

Just syntax error. To create your alist, use to following: (mapcar (lambda (x) (cons x 'k)) '(a b c d e))
adl's user avatar
  • 646
6 votes
Accepted

Org Mode. Set separation length between elements of a list

PDF export is done by exporting the org mode document to LaTeX and then processing the LaTeX document to PDF. There is nothing on the org mode side to control that spacing but org mode does provide ...
NickD's user avatar
  • 31.4k
6 votes
Accepted

Is there a way to spread a list like in Typescript or Golang?

You can use the backquote mechanism in combination with ,@ to splice the value of a variable into a list; or equivalently, use append: (setq variable '(1 2 3)) (1 2 3) (setq another-variable `(-2 -1 ...
NickD's user avatar
  • 31.4k
6 votes
Accepted

How to map or iterate over a list of files and set the result to `org-agenda-files`?

Without looking up any of the Org variables or functions you refer to, here's a guess at what you're trying to do. They both do the same thing: iterate over a list of file names, expanding them in ...
Drew's user avatar
  • 78.1k
6 votes

How to replace an element of a list?

(require 'cl-lib) (cl-substitute 100 1 '(3 2 1)) gives (3 2 100). (cl-substitute 100 1 '(3 2 1 2 1 2 1) :count 2) gives (3 2 100 2 100 2 1). (cl-substitute 100 "one" '("three" &...
Tobias's user avatar
  • 33.4k
6 votes
Accepted

How can I convert a string form of a list to an actual list?

ELISP> (read "(a b c)") (a b c) ELISP> (read "(9 . 3)") (9 . 3) If by (9 . 3) you mean you'd like a cons, then my answer would work. Note however if you actually would like ...
TerryTsao's user avatar
  • 1,226

Only top scored, non community-wiki answers of a minimum length are eligible