Hot answers tagged

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.6k
22 votes
Accepted

Why does a constant in `let` change with repeated calls?

Here is my answer to the identical question, appropriately edited: The Bad foo is self-modifying code. This is extremely dangerous. While the variable lst disappears at the end of the let form, its ...
sds's user avatar
  • 5,948
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
  • 75.8k
16 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
  • 32.6k
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
  • 21.8k
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
  • 5,948
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.7k
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,004
10 votes
Accepted

What's difference between + and - in the list in org mode

They make no difference, they are just a style of bullet and have no meaning. There are many different bullet types you can use, for sorted and non-sorted lists. You can use org-shiftright (s-right) ...
Jordon Biondo's user avatar
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.2k
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,333
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.2k
8 votes

Append lists smartly?

append does what the first part of your question asks: (append &rest SEQUENCES) Concatenate all the arguments and make the result a list. The result is a list whose elements are the ...
Dan's user avatar
  • 32.6k
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
  • 32.6k
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
8 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
  • 4,247
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,004
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
  • 146
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

How to iterate over JSON list and select one element

You can instruct json-read-from-string to parse JSON arrays as elisp lists by let binding json-array-type to list like so (let ((json-object-type 'plist) (json-array-type 'list)) (setq mylist ...
Iqbal Ansari's user avatar
  • 7,478
6 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
  • 5,948
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
  • 75.8k
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
  • 75.8k
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
  • 5,948
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.1k
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
  • 75.8k
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
  • 636
6 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.3k
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
  • 27.1k
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
  • 75.8k

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