[Top][All Lists]

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

Re: progv in scheme

From: Andrew Gwozdziewycz
Subject: Re: progv in scheme
Date: Tue, 13 Sep 2011 10:25:59 -0400

On Tue, Sep 13, 2011 at 9:54 AM, Panicz Maciej Godek
<address@hidden> wrote:
> Hello,
> Is there any clever way of binding values to the list of unknown
> symbols in scheme?
> In common lisp there is a form "progv" that takes the list of symbols
> and their corresponding values and binds them within the body of
> progv.
> It is possible to do it using eval, like this:
> (define (bind-and-eval symbols values body)
> (eval `((lambda ,symbols ,body) . ,values)
>      (interaction-environment)))
> (define-syntax let-symbols
> (syntax-rules ()
>  ((_ symbols values (body ...))
>   (bind-and-eval symbols values (quote (body ...))))))
> but using eval for this just seems too heavy. Is there any way of
> doing it that would be more legal?
> Best regards,
> M.

Seems likely that you could use `syntax-case' to create a `let` out of these:

(define-syntax progv
  (lambda (stx)
    (define (create-bindings syms vals)
      (datum->syntax stx (zip (syntax->datum syms) (syntax->datum vals))))
    (syntax-case stx ()
      ((_ symbols values body ...)
       (with-syntax ((bindings (create-bindings #'symbols #'values)))
          #'(let bindings
               (begin body ...)))))))

;; usage
(progv (foo bar baz) (1 2 3)
   (format (current-output-port) "Values: ~a ~a ~a\n" foo bar baz))
;; output: "Values: 1 2 3"

Hope this helps!


reply via email to

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