guile-user
[Top][All Lists]
Advanced

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

Re: Looping local binding


From: Neil Jerram
Subject: Re: Looping local binding
Date: Mon, 29 Oct 2007 22:12:04 +0000
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux)

Dmitry Dzhus <address@hidden> writes:

> Greetings! I've got the following lines in my code:
>
>  (let* ((method (option-ref options 'method "fundmatrix"))
>              ;; Command line options override ones in statement file
>              (right-bound
>               (string->number
>                (option-ref options 'right-bound (number->string 
> right-bound))))
>              (wave-number
>               (string->number
>                (option-ref options 'wave-number (number->string 
> wave-number))))
>              (subintervals
>               (string->number
>                (option-ref options 'subintervals (number->string 
> subintervals))))
>              (test-epsilon
>               (string->number
>                (option-ref options 'test-epsilon  (number->string 
> test-epsilon)))))
>              <body follows .. >)
>
> Looks like total mess, doesn't it?
>
> Is there any way to rewrite this somehow, say using a loop over a list
>
>       '(right-bound wave-number subintervals test-epsilon)
>
> ?
>
> Desperately I tried to perform local binding using `(let (map ..`, but
> obviously that couldn't work.

You definitely have to use a macro here, because the three uses of
"right-bound" (for example) refer to three different things (the outer
binding, the inner binding, and the options keyword).

(define-macro (let-options opts . body)
 `(let ,(map (lambda (opt-name)
               `(,opt-name (string->number
                            (option-ref options
                                        ',opt-name
                                        (number->string ,opt-name)))))
             opts)
    ,@body))

Usage would then be:

(let ((method (option-ref options 'method "fundmatrix")))
  (let-options (right-bound wave-number subintervals test-epsilon)
    <body follows .. >))

I haven't tested this, but it should be about right.  Please let me
know if you see problems.

It would be interesting to try to factorize this macro into its
constituent concepts.  One of those concepts is the idea of creating a
local binding (to shadow an outer binding) if some condition is true,
and not creating that local binding otherwise.  In other words it
feels like it would be nice to have something that expanded either to

(let ((var new-value))
  body)

or to

(begin
  body)

depending on whether an inner bindings is needed.  But I don't think
that's possible.

Regards,
        Neil





reply via email to

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