chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] possible lazy-let macro?


From: Thomas Chust
Subject: Re: [Chicken-users] possible lazy-let macro?
Date: Wed, 16 Nov 2005 15:06:17 -0000
User-agent: Opera M2/8.02 (MacPPC, build 2148)

Am 16.11.2005, 09:18 Uhr, schrieb Daishi Kato <address@hidden>:

[...]
Does anyone know of any existence of a lazy-let macro,
which does the following?

<convert from>

(lazy-let ([a (get-a)][b (get-b)])
  (if (condition) a b))

<into>

(if (condition) (get-a) (get-b))
[...]

Hello,

I haven't seen any macro like this, yet. As a workaround, I would
maybe use promises like this (transformation of the above example)
  (let ((a (delay (get-a))) (b (delay (get-b))))
    (if (condition) (force a) (force b)))

Of course this could fairly easily be automated using a non-hygienic
macro:
  (use match)

  (define-macro (lazy-let vars . body)
    `(let ,(map (match-lambda ((var xpr) `(,var (delay ,xpr)))) vars)
       ,@(map
           (let ((names (map car vars)))
             (rec (auto-force xpr)
               (cond
                ((list? xpr)
                 (map auto-force xpr))
                ((and (symbol? xpr) (memq xpr names))
                 `(force ,xpr))
                (else
                 xpr))))
           body)))

But it has the disadvantage, that you cannot transparently use set!
on the variables you declare with this form.

cu,
Thomas




reply via email to

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