replace parentheses of a list with quotation marks

Q&A's, tips, howto's
Locked
lyl
Posts: 44
Joined: Sun Mar 25, 2018 5:00 am

replace parentheses of a list with quotation marks

Post by lyl »

(setq a "time")
(setq b '(set xlabel a))

My question is:
how to design a function to transfer a list("b" in this example) into a string. That is to say, replace the parentheses of b with quotation marks to get "set xlabel "time"".

cameyo
Posts: 183
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Re: replace parentheses of a list with quotation marks

Post by cameyo »

Maybe in this way:

Code: Select all

(setq a "time")
;-> "time"
(setq b '(set xlabel a))
;-> (set xlabel a)
(define (lst-str lst)
  (setf (last lst) (eval (last lst)))
  (join (map string lst) " "))
(lst-str b)
;-> "set xlabel time"
or

Code: Select all

(define (lst-str lst)
  (setf (last lst) (append "\"" (eval (last lst)) "\""))
  (join (map string lst) " "))
(lst-str b)
;-> "set xlabel \"time\""

lyl
Posts: 44
Joined: Sun Mar 25, 2018 5:00 am

Re: replace parentheses of a list with quotation marks

Post by lyl »

Thank you, cameyo.
In my example, the expression that is to be evaled is the last element in the list which make it possible to use (setf (last lst)...) evaluating the expression. I wonder if there is an universal method to evluating any expression at any position in a list. Here I'd like give another example:

(setq lst '(set "outcome" (+ 1 2) dollars)

in which such a string: "set "outcome" 3 dollars" is wanted.

PS:
I think (list ...) may be a method, but thinking of those elements that can not be evaluated, it's a bit complicated.

Locked