Questions tagged [list]
is for the list object in Lisp programming. A list represents a sequence of zero or more elements. Emacs Lisp relies extensively on the list data structure; it is a fundamental building block. Emacs provides many facilities for building, modifying, extending, and efficiently handling list data structures.
116
questions
3
votes
3answers
231 views
When is it better to use the backquote, `(…), and when to use (list …)?
The docstring for quote says,
Quoting should be reserved for constants that will never be modified by side-effects, unless you like self-modifying code.
Are there cases in which it is the best ...
3
votes
1answer
74 views
What is the difference between these two lists?
I'm reading An Introduction to Programming in Emacs Lisp by Robert J. Chassell.
When the book is introducing setcar, it points out that when we use this function to a list, the list should be ...
3
votes
2answers
227 views
What difference does it make changing the order of arguments to 'append'
I want to understand the implications of append's property that "All arguments except the last one are copied, so none of the arguments is altered." (from the Elisp manual)
Say I have a list ...
2
votes
2answers
53 views
How to add a string at nth position of an ordered list of strings
The built-in function add-to-ordered-list is for symbols, not strings. I have a list of strings and need to add another string to the list at a specific nth position. How can I accomplish this?
TASK:...
0
votes
1answer
19 views
Access strange list data
I'm trying to parse the output of ledger-context-at-point for the account name. The output looks something like:
(acct-transaction
account
((indent " " 238117)
(status nil nil)...
3
votes
3answers
46 views
Retrieve alist from list of alists by value of specific key inside of it
I'm probably missing something simple here but... I've got a list of alists parsed from JSON using json-parse-string. The list of alists is like the following (details omitted for brevity):
( ((id . 1)...
2
votes
2answers
83 views
Assoc in a nested alist
I have the following nested alist:
(setq x '(foo . ((bar . ((chocolate . "edible") (gold . "inedible")))
(jar . "glass"))))
How can get entry (chocolate ...
0
votes
0answers
31 views
How can I add list arguments to key value pairs to defmacro?
I'm trying to use rg.
There is this explanation here:
(defmacro rg-define-search (name &rest args)
"Define an rg search functions named NAME.
ARGS is a search specification that defines ...
1
vote
3answers
66 views
Replace element in a list / add in case of absence, with custom test/key functions
add-to-list doesn't refresh item, it only checks for existence of item by equal or custom comparison function:
(add-to-list
'tramp-methods
'("gssh" (tramp-login-program "gcloud ...
1
vote
1answer
33 views
Function like `setcar` that doesn't change the value of the input function?
Early beginner here. I'm looking for a function that takes a list and changes only its first element, using some function or other. I realize setcar sort of does that, but it also changes the initial ...
3
votes
3answers
196 views
How to replace an element of a list?
Is there a way to replace an item in a list?
(replace-element-in-list (elem-src elem-dst ls &optional times comparison-fn))
;; ...
)
Example use:
(replace-element-in-list 1 100 '(3 2 1)) => ...
3
votes
1answer
40 views
How to add a value after a given value in parts of a list, returning a new list?
I have the following structure:
'((a (1 2 3 4))
(b (1 2))
(c (1 3 4 5))
I want to return a new list that's similar, but that has a 7 inserted after each 2:
'((a (1 2 7 3 4))
(b (1 2 7))
(c (1 ...
3
votes
1answer
129 views
How to map or iterate over a list of files and set the result to `org-agenda-files`?
looking for a little help with my function. I'm trying to create a loop-while that'll return a list of files back to org-agenda-files. It's just not working though and i keep getting errors. Any ideas ...
0
votes
2answers
88 views
Filter list by regexps
How can I get all elements of a list which don't match multiple
patterns? The patterns are given as a list.
For instance, I want all files without '("no" "nix") in them.
(setq se-...
1
vote
2answers
330 views
Is there a way to spread a list like in Typescript or Golang?
I have a variable with some common values between dap-mode debug templates and I want to use that variable along with other cons elements for :environment-variable in languages like Golang or ...
0
votes
3answers
160 views
How to replace a list within a list?
What would be the an efficient/concise way to replace a sub-list with another list?
Example function call:
(replace-list-list-with-list '("A" "B" "C" "D") '("B" "C") '("NEW" "TEXT"))
Would result ...
2
votes
1answer
37 views
Why does Flycheck complain “mapcar called for effect” when my function returns the result of mapcar by design?
Since installing Flycheck I've been bombarded with complaints about the elisp functions I've used reliably for years. This is the first one I can't understand. Here my function is designed to return ...
3
votes
1answer
120 views
How does a list end in nil?
I believe Emacs lists have nil at their end.
That said, I was trying to test examples.
Can anyone tell the difference between a and b?
(setq a '(foo))
(setq b '(foo nil))
;; Returns nil and (nil)...
0
votes
1answer
39 views
Print matching strings line by line
I was trying to use this code shared by @JordonBiondo over here to collect regex matches in a list.
(defun matches-in-buffer (regexp &optional buffer)
"return a list of matches of REGEXP in ...
1
vote
0answers
41 views
Emacs freezes when using split-string in recursive function
I'm trying to figure out why my emacs keeps freezing when running this set of functions to split a string into a list of pairs. Is there some significant overhead happening when using string-split? Is ...
1
vote
2answers
195 views
Get first n elements of a list
How can I get the first N elements of a list?
I know I can use subseq:
(subseq '("one" "two" "three" "four" "five") 0 2)
Yet when I use subseq, it produces one of the following results:
on the ...
1
vote
1answer
43 views
Would (cycle-list <ls>) be a bad idea?
Would you consider something like this a bad practice?
(defun cycle-list (ls &optional backward?)
"Given any list, returns the next element each time it is called."
(let* ((last-pos (- (...
4
votes
1answer
222 views
Circular list use of “#”
Looking at this, I see
(setq x '#1=(a #1#))
but this evaluates to (a #0). As I understand, this should be (a #1=(a #1#)). But then what syntax makes #0 mean #1=(a #1#)? In general what is # doing in ...
2
votes
1answer
38 views
Make forward-list search for specific symbol only
Is there a way to make forward-list and backward-listsearch for specific symbols (such as 「」『』) only?
I have code that make use of these functions to iterate through 「」『』 pairs in plain text. But ...
1
vote
2answers
200 views
How to subtract 1 from the nth element of a list of numbers?
How do I subtract 1 from the second element of the list (3 6)?
(setq x '(3 6))
(- (nth 1 x) 1)
The above quote does the math but does not return a modified list.
-1
votes
2answers
51 views
Add a number to every item in list
How do I add a number, say 2, to every item in a list?
(setq x '(1 2))
(+ 2 x)
(mapcar '2+ x)
(loop for i in x
do (+ 2 i))
(dolist (i x)
(+ 2 i))
None of the above work.
3
votes
3answers
324 views
Move point inwards while counting number of nested parentheses without throwing an error
I would like to write a function that counts the number of nested parentheses while moving the cursor to the innermost bracket without throwing an error.
(defun count-and-move ()
(interactive)
...
2
votes
5answers
542 views
Remove multiple elements from a list
Given '(a b c d), how would I remove b and c to end up with '(a d)? Is there a "right" way to remove multiple, distinct elements from a list?
Obviously, one could do (delete 'c (delete 'b '(a b c d))...
3
votes
2answers
178 views
Apply different function to each element in list?
I have a time-stamp of a video formatted as "00:00:00,000" such that "%H:%M:%s,%3N".
I am want to eventually use this timestamp as part of an in-interval-p function where I take two timestamps and a ...
1
vote
2answers
43 views
How to check if a list ends with another list?
Currently I'm using a fairly inefficient way to check if one list uses another list at it's end. Is there a more efficient way to do this?
(defun is-in-list (ls-haystack ls-ends-with)
(catch 'found
...
3
votes
2answers
437 views
How to check if two lists share any elements?
Besides looping over list items and comparing, what is a good way in elisp to check if there are any shared items between two lists?
For example, in Python this can be done with sets or list ...
1
vote
1answer
59 views
Is function `list` equivalent to using `cons` with a final cdr of `nil`?
I'm trying to explain Elisp to 5th and 6th graders and I tell them that (+ 1 2) evaluates to 3 and not (3). So if I do want something that evaluates to (3) would I just evaluate ((+ 1 2))? No! This ...
3
votes
1answer
4k views
Melpa - 'Failed to download melpa'
I am on a fresh install of emacs 26.3, and I have the following in my init.el:
(require 'package)
(add-to-list 'package-archives
'("melpa", "http://melpa.org/packages/") t)
(package-...
0
votes
2answers
63 views
Representing leading zeroes in a list
I would like to have this table
#+NAME: addition-table
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---+----+----+----+----+----+----+----+----+----+----|
| 0 | 00 | 01 | 02 | 03 | 04 | ...
5
votes
1answer
718 views
Are the graphical depictions in docs drawed in manual labour?
There are tons of pretty graphical depictions in the emacs(or elisp) docs such as
x1:
-------------- -------------- --------------
| car | cdr | | car | cdr | | car | ...
1
vote
1answer
109 views
Map each element of a list over every element of another list
Let's assume I've got two lists: ("A" "B" "C" "D") and ("+" "-"). I want to concatenate each element of the first list with every element of the second to get ("A+" "A-" "B+" "B-" ... ). Is there a ...
0
votes
1answer
43 views
Inline elements of variable list value in rx() macro call [duplicate]
Is there a more compact way to make use of the rx macro in cases where we want to inline a list of strings from a variable? Such as
(defconst c++20-new-keywords-list
(list
"char8_t"
"import"
...
2
votes
1answer
195 views
Elisp: How to accumulate function return into a list
I have this small snippet of Elisp code that decodes kernel traces:
(defvar projbase "/my/proj/base")
(setq my_shell_output
(substring
(shell-command-to-string (concat "/home/demetra/scripts/...
1
vote
2answers
165 views
Orgmode automatically increment an unordered list
I use org-mode for planning projects and tasks, etc. Say I am reading a book and I want to track my progress chapter by chapter. I want to create a list of repetitive elements with an incremented ...
2
votes
1answer
70 views
trying to make a self-modifying function [duplicate]
I'm trying to make a function that returns a list of perfect numbers:
A perfect number is a positive integer that is equal to the sum of its proper positive divisors
For instance, 28 is perfect ...
7
votes
3answers
651 views
Removing the last element of a list
Is there a simpler way to remove the last element of a list than this?
(setq list (reverse (cdr (reverse list))))
6
votes
1answer
178 views
Shortening trees list with (setcdr (nthcdr 2 trees) nil)
I am starting to learn elisp and working through this tutorial https://www.gnu.org/software/emacs/manual/html_node/eintr/kill_002dnew-function.html
and I do not understand this basic example:
(setq ...
1
vote
1answer
58 views
midnight-mode add-to-list Format specifier doesn't match argument type
I've been trying to get midnight-mode to add to the following list. I have a feeling it has to do with an elisp error in my config I'm not seeing. It works fine with setq, but with add-to-list I get a ...
2
votes
1answer
264 views
How to write a list literal with conditional items? [duplicate]
Given a list literal '(1 2 3)
is it possible to conditionally include parts of the list?
eg:
'(1 2 3
(when thing-is-true '(10 11 12)))
4 5 6)
Which would result in
'(1 2 3 4 5 6) or '(1 2 3 ...
3
votes
1answer
660 views
Org Mode. Set separation length between elements of a list
I have noticed there is a big gap between the elements of a list when exporting to PDF. Is there a way to modify it?
0
votes
1answer
69 views
List variable that is set to one list or another depending on condition
I am trying to create a list variable that can be set to one list or another depending on a condition. What I have so far is as follows.
(let ((l (if local list1 list2)))
; Append items to the list
...
0
votes
1answer
51 views
How can i add same value to every item in list, turning it into alist?
I tried to use this code, but it returns (k k k k k)
(mapcar (lambda (x) 'cons x '(k))
'(a b c d e))
What am i doing wrong here? Why doesn't cons work?
4
votes
4answers
613 views
Map function onto nested list?
Q: how can I map a function onto elements of nested lists?
For flat lists, we can use mapcar to apply a function to each
element of the list:
(setq flat '("kittens" "puppies" "otters" "bunnies"))
(...
9
votes
1answer
728 views
When to use quote for lists? Modifying quoted lists in Elisp
The Common Lisp Hyper Spec says:
The consequences are undefined if literal objects (including quoted objects) are destructively modified.
This is further motivated at the Constant Modification ...
2
votes
2answers
203 views
Evaluating symbol in function arguments affected by destructive operations?
While evaluating a function call, the arguments is from left to right. For example,
(let ((x '(1 2 3)))
(list (nreverse x) x))
;; => ((3 2 1) (1))
The first argument (nreverse x) evaluates to (...