Question

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?

Was it helpful?

Solution

It's actually as easy as it gets:

(list 'quote (list 'quote var))

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top