Is it possible to generate (quote (quote var)) or ''var dynamically?

╄→гoц情女王★ 提交于 2019-12-23 05:34:09

问题


In question Using AND with the apply function in Scheme , I proposed an solution to apply "and" onto a list of atoms (i.e. s-exp which is neither null, nor pair.) in PLT-Scheme 372.

Welcome to DrScheme, version 372 [3m].
Language: Textual (MzScheme, includes R5RS).
> (eval (cons 'and (list ''#f ''#f ''#t)))
#f
> (eval (cons 'and (list ''a ''b ''c)))
c

But right after that I realized that: it's not easy to generate (quote (quote var)) or ''var dynamically. To be specific, the following code will not work:

(define (my-quote lst)
  (cond
    ((null? lst) '())
    (else
     (cons (quote (car lst))
           (my-quote (cdr lst)))
     )))

Welcome to DrScheme, version 372 [3m].
Language: Textual (MzScheme, includes R5RS).
> (my-quote (list 'a 'b 'c))
((car lst) (car lst) (car lst))

as (car lst) will not be evaluated in the first place; And even if I (let ((var (car list))) (quote var)), it will not work, either, as var will not be evaluated.

My question is, is there some sort of mind trap in my attempting to do this?


回答1:


It's actually as easy as it gets:

(list 'quote (list 'quote var))



回答2:


Is this what you want?

(define (my-quote lst)
  (map (lambda (x) `',x) lst))

then

(cons 'and (my-quote (list 'a 'b 'c)))
=> '(and 'a 'b 'c)

If you want more quotes, just add them in the procedure:

(define (my-quote lst)
  (map (lambda (x) `'',x) lst))

(cons 'and (my-quote (list 'a 'b 'c)))
=> '(and ''a ''b ''c)


来源:https://stackoverflow.com/questions/20797505/is-it-possible-to-generate-quote-quote-var-or-var-dynamically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!