6

I would like to convert these two strings

"(a b c)"

"(9 . 3)"

to these

(a b c)

(9 . 3)

I'd had some luck with the first one evaluating this,

(mapcar 'intern (split-string (string-trim "(a b c)" "(" ")"))) basically, removes parentheses from string, splits it to ("a" "b" "c"), then calls intern to turn all the elements to symbols and yields (a b c). When evaluating this with "(9 . 3)" though, It escaped all elements and yields (\9 \. \3), this however, is not the same as (9 . 3) that I wanted.

Drew
  • 75,699
  • 9
  • 109
  • 225
C11g
  • 333
  • 1
  • 7
  • 1
    Please see my answer below. Note that your question is related to the fact that: In lisp, code (the string), code (in memory), data (list, etc.), read & print representations, how lisp parse its code are all closely related. – TerryTsao May 18 '21 at 09:51
  • Yes, I saw your answer and it solved my problem wonderfully, you answered so quick that I had to wait 10 minutes to accept the answer. – C11g May 18 '21 at 09:54

1 Answers1

6
ELISP> (read "(a b c)")
(a b c)

ELISP> (read "(9 . 3)")
(9 . 3)

If by (9 . 3) you mean you'd like a cons, then my answer would work. Note however if you actually would like a dot, my answer not so much.

TerryTsao
  • 1,176
  • 4
  • 18
  • 3
    Probably worth mentioning the similar `read-from-string` too, and/or linking to [`(info "(elisp) Input Functions")`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Input-Functions.html). – Basil May 18 '21 at 13:07