>>>>> Hans Nowak <[EMAIL PROTECTED]> writes:

    > #;1> (define magic 42) #;2> (define s '(foo bar ,magic))

    [...]

    > The following doesn't work:

    > #;4> `s s #;5> `,s (foo bar (unquote magic))

    > ...and I understand *why* they don't work, but I can't figure
    > out how to take s and transform it into (foo bar 42).

quasiquote is syntax - it happens at compile-time, so to
apply it to dynamic data at runtime you need to call EVAL:

  (eval (list 'quasiquote s))
  ; => (foo bar 42)

EVAL may be overkill for something this simple - an
alternative would be to write a simple substitution
function:

  (use lolevel)

  (define (replace-unquotes x)
    (if (not (pair? x))
        x
        (if (and (pair? (cdr x)) (null? (cddr x)) (eq? 'unquote (car x)))
            (global-ref (cadr x))
            (cons (replace-unquotes (car x))
                  (replace-unquotes (cdr x))))))

  (replace-unquotes s)
  ; => (foo bar 42)

You'd of course have to extend this if you want to handle
nested quasiquotes and unquote-splicing.

-- 
Alex


_______________________________________________
Chicken-users mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to