chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Stupid backquote/unquote question


From: Alex Shinn
Subject: Re: [Chicken-users] Stupid backquote/unquote question
Date: Thu, 21 Feb 2008 16:16:31 +0900
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1.50 (darwin)

>>>>> Hans Nowak <address@hidden> 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]