Comments (3)
You can actually, have association lists as lists of two elements or list of cons cells. It works with both, your functions and mine are correct. Here na example:
ELISP> (defun plist->alist (plist)
(if (null plist)
'()
(cons
(list (car plist) (cadr plist))
(plist->alist (cddr plist)))))
plist->alist
ELISP>
ELISP> (plist->alist (list :x 10 :y 20 :name "point"))
((:x 10)
(:y 20)
(:name "point"))
ELISP> (assoc :x (plist->alist (list :x 10 :y 20 :name "point")))
(:x 10)
ELISP> (assoc :y (plist->alist (list :x 10 :y 20 :name "point")))
(:y 20)
ELISP>
The problem with list of cons cells is that in the first code below, it becomes a single list, which makes hard to distinguish between a "list" or cons cell (Every list is a cons cell, of the head element and the rest of the list).
ELISP> (cons :x '(1 2 3 4))
(:x 1 2 3 4)
ELISP> '(:x . (1 2 3 4))
(:x 1 2 3 4)
ELISP> (list :x '(1 2 3 4))
(:x
(1 2 3 4))
from emacs-elisp-programming.
OK, I agree: should have read the manual more carefully. However, as the
manual says, reverse lookup with rassoc will fail on your version:
ELISP> (setq my-alist '((:a . 1) (:b . 2)))
((:a . 1)
(:b . 2))
ELISP> (setq yr-alist '((:a 1) (:b 2)))
((:a 1)
(:b 2))
ELISP> (rassoc 2 my-alist)
(:b . 2)
ELISP> (rassoc 2 yr-alist)
nil
---Fran
On 7 December 2015 at 04:54, Caio Rordrigues [email protected]
wrote:
You can actually, have association lists as lists of two elements or list
of cons cells. It works with both, your functions and mine are correct.
Here a example:ELISP> (defun plist->alist (plist)
(if (null plist)
'()
(cons
(list (car plist) (cadr plist))
(plist->alist (cddr plist)))))
plist->alist
ELISP>
ELISP> (plist->alist (list :x 10 :y 20 :name "point"))
((:x 10)
(:y 20)
(:name "point"))ELISP> (assoc :x (plist->alist (list :x 10 :y 20 :name "point")))
(:x 10)ELISP> (assoc :y (plist->alist (list :x 10 :y 20 :name "point")))
(:y 20)ELISP>
The problem with list of cons cells is that in the first code below the it
becomes a single list, which makes hard to distinguish between a "list" or
cons cell (Every list is a cons cell, of the head element and the rest of
the list).ELISP> (cons :x '(1 2 3 4))
(:x 1 2 3 4)ELISP> '(:x . (1 2 3 4))
(:x 1 2 3 4)ELISP> (list :x '(1 2 3 4))
(:x
(1 2 3 4))—
Reply to this email directly or view it on GitHub
#12 (comment)
.
from emacs-elisp-programming.
Thanks for the contribution. Ok I will add your function, but I will call this list of cons pair
of clist '((a . b) (x . y)) and the lists of two elements of alist to reduce the ambiguity '((a b) (x y)).
from emacs-elisp-programming.
Related Issues (9)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from emacs-elisp-programming.