Page 1 of 1

replace parentheses of a list with quotation marks

Posted: Wed Sep 25, 2019 2:28 am
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"".

Re: replace parentheses of a list with quotation marks

Posted: Wed Sep 25, 2019 3:37 pm
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\""

Re: replace parentheses of a list with quotation marks

Posted: Thu Sep 26, 2019 6:42 am
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.