newLISP Fan Club

Forum => newLISP in the real world => Topic started by: lyl on September 24, 2019, 07:28:48 PM

Title: replace parentheses of a list with quotation marks
Post by: lyl on September 24, 2019, 07:28:48 PM
(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"".
Title: Re: replace parentheses of a list with quotation marks
Post by: cameyo on September 25, 2019, 08:37:18 AM
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""
Title: Re: replace parentheses of a list with quotation marks
Post by: lyl on September 25, 2019, 11:42:43 PM
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.