replace parentheses of a list with quotation marks

Started by lyl, September 24, 2019, 07:28:48 PM

Previous topic - Next topic

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

#1
Maybe in this way:
(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
(define (lst-str lst)
  (setf (last lst) (append """ (eval (last lst)) """))
  (join (map string lst) " "))
(lst-str b)
;-> "set xlabel "time""

lyl

#2
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.