Question

If I have a list like this (define lst '((,(car '(1 2)) (1 2)) (,(car '(3 4)) (3 4))) ) and I try to 'evaluate' the expression (i.e. take as result '((1 (1 2)) (3 (3 4)))) I obtain the same list that I have.

I know that if I use (quoasiquote ((,(car '(1 2)) (1 2)) (,(car '(3 4)) (3 4))) I obtain what I'm looking for, but the problem is in an execution with an iterative method, where (I think) I'm not capable of take only the values, without being a list. (i.e. take only the second part of the quoasiquote expression).

For example, if I use for/list and I do (list-ref lst 0), I obtain '(,(car '(1 2)) (1 2)), when I want (,(car '(1 2)) (1 2)) for use it in quoasiquote function.

How can I obtain the expression inside a list and evaluate it?

Thank you.

Était-ce utile?

La solution

So when you quote data it's like single quotes in some popular Algol dialects like perl. eg print '$_'; actually prints $_ and not the value the variable $_ represents. If you would have used double quotes the variables in the string is expanded to it's value.

In Scheme we have (quote x) which is 'x. Nothing in it will ever going to become evaluated since it's quoted with '. If it were `x, which is the same as (quasiquote x) then Scheme looks for ,expression (same as (unquote expression) ) and ,@expression (same as (unquote-splicing expression) ). These quote forms are NOT procedures but macros. It transforms your static code to different static code.

So (begin (define test 10) `((1 2) (3 ,(+ test 4)))) ; ==> ((1 2) (3 14))

What Scheme really does is transform it into (list '(1 2) (list 3 (+ test 4))), while if it was `((3 ,(+ test 4)) (1 2)) it turns into (cons (list 3 (+ test 4)) '((1 2))) since the tail can be a constant but the head cannot if it needs to have a evaluated tail.

I get the impression from you question that you think you can store the text and expand them later like (let ((x '((1 2) (3 ,(+ test 4))))) `x) but that will not work since quasiquote is a macro and thus it will evaluate to x. and (let ((x '((1 2) (3 ,(+ test 4))))) `,x) would evaluate to ((1 2) (3 ,(+ test 4))).

Autres conseils

I'm pretty confident that we have an XY-problem here because what you describe is pretty unusual. Also, the only solution I imagine here is using eval, which again shows that you're probably heading into the wrong direction.

But here's a little attempt at what I believe you want:

(define lst '((list (car '(1 2)) '(1 2))
              (list (car '(3 4)) '(3 4))))

(define-namespace-anchor nsa)
(define ns (namespace-anchor->namespace nsa))

(for/list ((i lst)) 
  (define e (eval i ns))
  (printf "evaluating ~a    to    ~a\n" i e)
  e)

which will print

evaluating (list (car '(1 2)) '(1 2))    to    (1 (1 2))
evaluating (list (car '(3 4)) '(3 4))    to    (3 (3 4))

and evaluate to

'((1 (1 2)) (3 (3 4)))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top