Hugal31
Hugal31

Reputation: 1768

How to change content of quoted value in Guile

I have an symbol that evaluates to (quote ("all")). I would like to append "tests" to the end of the list, and get (quote ("all" "tests")) but I didn't find how to :

(define make-flags ''("all"))
(append make-flags '("tests")) ; Resolves to (quote ("all") "tests")

I suppose I would have to remove the quote by evaluate the make-flags twice and re-quote it, but I didn't find how to.

Upvotes: 0

Views: 113

Answers (2)

Sylwester
Sylwester

Reputation: 48745

The second you evaluate ''("all") you get the list (quote ("all")) and it is not a quoted list at all. This is a list of to elements, the symbol quote and the list ("all"). If you want to add an element to the second element you do it by recreating the outer list and replacing the second with the new list with the added element:

(define (add-second-element ele lst)
  `(,(car lst) (,@(cadr lst) ,ele) ,@(cddr lst)))
    
(add-second-element 'goofy '((donald dolly) (mickey) (chip dale)))
; ==> (donald (mickey goofy) (chip dale))

(add-second-element "tests" ''("all"))
; ==> (quote ("all" "tests")) 

If you're not too familiar with quasiquote it's possible to do it without since quasiquote is just fancy syntax sugar for cons and append:

(define (add-second-element-2 ele lst)
  (cons (car lst) (cons (append (cadr lst) (list ele)) (cddr lst))))

(add-second-element-2 'goofy '((donald dolly) (mickey) (chip dale)))
; ==> (donald (mickey goofy) (chip dale))

Of course if the first element is always quote and there are only two elements these can easily be simplified in both versions.

Upvotes: 1

Óscar López
Óscar López

Reputation: 236004

Yes, you'd need to remove the quote first. Try this:

(define make-flags ''("all"))
`'(,(append (cadr make-flags) '("tests")))
=> ''("all" "tests")

It works because make-flags is just a list of this form: (quote (quote ("all"))) and we can navigate it in the usual way with car and cdr.

Upvotes: 1

Related Questions