Hot answers tagged

23

You can also just use append. (append '("a" "b" "c") '("d" "e" "f"))


22

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 a list that is not a dotted list or a circular list) is something that is listp and also has nil as its last cdr. That is, a list XS is proper if (cdr (last XS)...


21

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 initial value persists in the function object, and that is the value you are modifying. Remember that in Lisp a function is a first class object, which can be ...


15

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 (readcharfun)); else val = read0 (readcharfun); read1 (readcharfun, &ch, 0); if (ch == ')') { if (doc_reference == 1) ...


12

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 already contains what you are adding, while add-to-list cannot control buffer-local status of the symbol you are modifying. The question you meant to ask These ...


11

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 poke around inside the list) and raises an error if the list isn't 2 long.


10

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) and org-shiftleft (s-left) to cycle through bullet types to see them all. It is up to you to decide which one you would like to use. See also the docs of org-...


10

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 word about phils' comment: If it's safe to modify the original list structure, then nbutlast will be slightly more efficient (n.b. you still need to assign ...


9

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), use the command ‘M-x picture-mode’ to enter Picture mode. In Picture mode, editing is based on the “quarter-plane” model of text, according to which ...


8

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 cdr of (nthcdr 2 trees) is set. If you want to assign the intermediate value to a variable for better understanding you should introduce a new one, e.g., trees-...


6

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 your question, but if you have a few minutes to kill, keep reading. ;-) Note that Emacs maintainers have traditionally eschewed CL (starting with the original ...


6

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 elements of all the arguments. Each argument may be a list, vector or string. The last argument is not copied, just used as the tail of the new list. delete-...


6

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 (json-read-from-string "[{\"name\": \"Adam\", \"id\": \"1\"}, {\"name\": \"Eve\", \"id\": \"2\"}]"))) ...


6

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 want to hear the answer. ;-) And no, they are not just rephrasing your question. The answer is, as others have said, that the return value is useful beyond ...


6

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)) list))) (remove-nth-element 0 (list 1 2 3 4 5)) (2 3 4 5) (remove-nth-element 1 (list 1 2 3 4 5)) (1 3 4 5) (remove-nth-element 2 (list 1 2 3 4 5)) (1 2 4 5) ...


6

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 (ele) (> (length ele) 2)) it) (mapcar #'car it))) A quick test suite: (ert-deftest nothing () (should-not (find-duplicates '()))) (ert-deftest no-...


6

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 (gethash x ht 0))) (maphash (lambda (key value) (when (> value 1) (push key ret))) ht) ret)) (get-...


6

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: -zip-with from dash.el (Dash ≥ 1.1.0) all share the limitation you describe: all the map functions seem to quit at the shortest list The philosophical ...


6

Just syntax error. To create your alist, use to following: (mapcar (lambda (x) (cons x 'k)) '(a b c d e))


5

Maybe this will clear up some of the confusion: Your function initilize does not initialize variable example. It sets it to a particular cons cell - the same cons cell each time it is called. The first time initilize is called, the setq assigns example to a new cons cell, which is the result of evaluating '(3). Subsequent calls to initilize just reassign ...


5

You can use (setq example (list 3)) to avoid this error. What happens is init assigns an object that initially contains (3) to example. It sets the value of the object only once. Subsequently, you modify this value. Here's your example in C++, if you understand that better: #include <stdio.h> #include <string.h> char* example; char* storage = ...


5

You can use the apply function to pass a function arguments contained in a list as separate arguments. For example: (apply #'format "%04d-%02d-%02d" '(2017 8 21)) That way you can store the list in a variable or compute it with a function call, etc. (let ((best-day-ever '(2017 8 21))) (apply #'format "%04d-%02d-%02d" best-day-ever))


5

Simply (pop (nthcdr n my-list)): (let ((x '(a b c d e))) (list (pop (nthcdr 2 x)) x)) ;; => (c (a b d e))


5

make-symbol returns an uninterned symbol. That means you get a symbol with name, function cell, value cell, and property list but the symbol is not registered in the global obarray. Therefore, you cannot use it for function evaluation with the usual parenthesis notation. If you replace make-symbol by intern the symbol is also registered in the obarray and ...


5

You want to apply the solution recursively. For example: (cl-labels ((upcase-nested (x) (cond ((consp x) (mapcar #'upcase-nested x)) ((stringp x) (upcase x)) (t x)))) (mapcar #'upcase-nested '(("kittens" ("spam") "puppies") ("otters" "bunnies" ("spam" ("spam" ("spam" ("...


5

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", "https://melpa.org/packages") If we put this into IELM, let's see what we get: ELISP> '("melpa", "https://melpa.org/packages") ("melpa" (\, "https://melpa.org/packages")...


4

Note, the problem with the original version of paragraph-fill is that it calls fill-region if the region is active. This merges all items into one filled item instead of several filled items. There follows a command fill-region-paragraphs that fills each paragraph with the non-interactive version of fill-paragraph instead. Thus the items in the region are ...


4

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 well. Here's a defun you might use to mark the subtree and then change it to a list: (defun org-subtree-make-list () (interactive) (org-mark-subtree) (org-...


4

We don't actually need to keep track of the last N commands, just the last command, and how many times it's repeated. This code will send a message when a command is called too many consecutive times. (defvar my-last-fn-repeat-count 0 "How many consecutive times the last function has been called.") (defvar repeat-limit 5 "When a command has repeated ...


4

The function that comes to my mind at least would be cl-member using a string-match-p test. Here's a short example that shows how to use it: ELISP> (setq testing '("an apple" "a pear" "a grape")) ("an apple" "a pear" "a grape") ELISP> (cl-member "apple" testing) nil ELISP> (cl-member "an apple" testing) nil ELISP> (cl-member "an apple" testing ...


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