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.