Given the "everything's-a-list" homoiconic approach to code and data in Lisp, I'm having a hard time understanding how to manipulate alists in Emacs Lisp. In particular, how can I convert an alist to an ordinary list of two-element lists, and vice versa? It almost seems like alists are a special type of their own which can't be manipulated the way regular lists are.
Specific example: I'm trying to bind a key in my .emacs
file to create a frame of a certain size with a programatically generated name, as follows:
(make-frame '((name . "Blarg") (width . 80) (height . 24)))
This works fine as long as "Blarg" is a constant string, but because of the quote at the beginning of the alist, I can't put any code that evaluates to a string in place of "Blarg". What I would like to do is build up a list by cons
ing the symbols and integers for the width and height, then add-list
the name on to the front, and pass the whole thing to make-frame
. But how do I convert the resulting data structure to an alist?
Specific answers on how to get make-frame
to do what I want would of course be appreciated, but as my title indicates, I'm hoping for a more general explanation of how to manipulate alists and convert them to/from regular lists.
Just my two cents on constructing alists:
(pairlis '(name width height)
'("Blarg" 80 24))
;; => ((name . "Blarg") (width . 80) (height . 24))
@wvxvw answered your general question. But note that what is described is not converting an alist to a list or vice versa.
And in general you do not need to do any such "conversion". You can instead work directly with alists -- they are lists, after all.
Which brings me to the answer to your make-frame
question, which is this, where foobar
holds the name you want:
(make-frame `((name . ,foobar) (width . 80) (height . 24)))
or if you prefer:
(make-frame (cons (cons 'name foobar) '((width . 80) (height . 24))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With