Why Nostr? What is Njump?
2025-03-06 21:39:00
in reply to

Pavel Korytov :emacs:☮️ on Nostr: nprofile1q…r93qq I'll explain in a bit more detail, just in case. `append' ...

I'll explain in a bit more detail, just in case. `append' concatenates lists, e.g.

(append '(1 2) '(3 4)) => (1 2 3 4)

You can use `push':

(setq my/test '((:b 2) (:c 3)))
(push '(:a 1) my/test) => ((:a 1) (:b 2) (:c 3))

This adds the item to the front of the list, because Lisp lists are just cons cells:

'((:a 1) . ((:b 2) . ((:c 3) . nil))) => ((:a 1) (:b 2) (:c 3))

So, adding any item to a list is just (cons <new-item> old-list). Which is what `push' does, see the docstring:

> This is morally equivalent to (setf PLACE (cons NEWELT PLACE)),
> except that PLACE is evaluated only once (after NEWELT).

`append' is a more complex function, which iterates over every argument and concatenates the result into one list.

Hence, unlike `push', it is O(n), which is also the complexity of adding an element to the end of list in Lisp, so it's almost never done. But, if you want that, nothing stops you from doing as suggests:

(append list (list plist))
`(,@list ,plist)
or
(append list `(,plist))

Unless you're doing that in a loop, in which case you'd be better off with `push' and `nreverse'.

Also, json.el prefers alists to plists, although it tries to do its best. E.g.:

(json-encode '(:a 1 :b 2 :c 3)) => {"a": 1, "b": 2, "c": 3}
(json-encode '((:a 1 :b 2 :c 3))) => {"a": [1, "b", 2, "c", 3]}

Because the second object is indistinguishable from:

(json-encode '((:a . (1 :b 2 :c 3)))) => {"a": [1, "b", 2, "c", 3]}

And with alists, there's no such confusion:

(json-encode '((:a . 1) (:b . 2) (:c . 3))) => {"a": 1, "b": 2, "c": 3}
(json-encode '(((:a . 1) (:b . 2) (:c . 3)))) => [{"a": 1, "b": 2, "c": 3}]
Author Public Key
npub15zlt94rw03ze79fe2r8n4u7xu2d6r5ck6zxeaykfx97qdp7fnd2svxfrsl