unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Looping local binding
@ 2007-10-29 16:28 Dmitry Dzhus
  2007-10-29 22:12 ` Neil Jerram
  0 siblings, 1 reply; 7+ messages in thread
From: Dmitry Dzhus @ 2007-10-29 16:28 UTC (permalink / raw)
  To: guile-user

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.
-- 
Happy Hacking.

Dmitry "Sphinx" Dzhus
http://sphinx.net.ru



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Looping local binding
  2007-10-29 16:28 Looping local binding Dmitry Dzhus
@ 2007-10-29 22:12 ` Neil Jerram
  2007-11-03 10:46   ` Dmitry Dzhus
  0 siblings, 1 reply; 7+ messages in thread
From: Neil Jerram @ 2007-10-29 22:12 UTC (permalink / raw)
  To: Dmitry Dzhus; +Cc: guile-user

Dmitry Dzhus <mail@sphinx.net.ru> 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



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Looping local binding
@ 2007-10-30 17:03 Marco Maggi
  2007-11-03 10:46 ` Dmitry Dzhus
  0 siblings, 1 reply; 7+ messages in thread
From: Marco Maggi @ 2007-10-30 17:03 UTC (permalink / raw)
  To: guile-user

(define-module (doit)
  #:use-module (ice-9 format)
  #:use-module (ice-9 getopt-long)
  #:use-module (ice-9 syncase))

(define-syntax let-numeric-options
  (syntax-rules ()
    ((_ ?options-list (?option ...) ?body ...)
     (let ((?option (string->number
		     (option-ref ?options-list
				 (quote ?option)
				 (number->string ?option))))
	   ...)
       ?body ...))))

(define-syntax print-option
  (syntax-rules ()
    ((_ ?option)
     (format #t "~A~/= ~A~%" (quote ?option) ?option))))

(define *grammar*
  '((method		(value #t))
    (right-bound	(value #t))
    (wave-number	(value #t))
    (subintervals	(value #t))
    (test-epsilon	(value #t))))

(define (test args)
  (let* ((options	(getopt-long args *grammar*))
	 (method	(option-ref options 'method "fundmatrix"))
	 (right-bound	1)
	 (wave-number	2)
	 (subintervals	3)
	 (test-epsilon	4)
	 (separator	(lambda ()
			  (display (make-string 30 #\-))
			  (newline))))
    (let-numeric-options options
      (right-bound wave-number subintervals test-epsilon)
      (separator)
      (print-option method)
      (print-option right-bound)
      (print-option wave-number)
      (print-option subintervals)
      (print-option test-epsilon)
      (separator))))

(test '("the-command"))
(test '("the-command" "--right-bound=123" "--subintervals=456"))
(test '("the-command" "--subintervals=456"
"--test-epsilon=123"))
(test '("the-command"
"--method=minkowsky-space-time-chess-opening"
	"--subintervals=456" "--test-epsilon=123"))

;; end of file

--
Marco Maggi

"Now feel the funk blast!"
Rage Against the Machine - "Calm like a bomb"




_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Looping local binding
  2007-10-29 22:12 ` Neil Jerram
@ 2007-11-03 10:46   ` Dmitry Dzhus
  0 siblings, 0 replies; 7+ messages in thread
From: Dmitry Dzhus @ 2007-11-03 10:46 UTC (permalink / raw)
  To: guile-user

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

Seems to work quite fine, thanks a lot!
-- 
Happy Hacking.

Dmitry "Sphinx" Dzhus
http://sphinx.net.ru



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Looping local binding
  2007-10-30 17:03 Marco Maggi
@ 2007-11-03 10:46 ` Dmitry Dzhus
  2007-11-03 11:12   ` Neil Jerram
  2007-11-03 18:33   ` Keith Wright
  0 siblings, 2 replies; 7+ messages in thread
From: Dmitry Dzhus @ 2007-11-03 10:46 UTC (permalink / raw)
  To: guile-user

That's terrific, Marco, and that code is almost clear to me.

I'm now strongly convinced that I should introduce myself to Scheme
macro forms closer. What may a recommended definite guide to it? (I
read SICP, but that book concentrates on other kind of things).
-- 
Happy Hacking.

Dmitry Dzhus
http://sphinx.net.ru



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Looping local binding
  2007-11-03 10:46 ` Dmitry Dzhus
@ 2007-11-03 11:12   ` Neil Jerram
  2007-11-03 18:33   ` Keith Wright
  1 sibling, 0 replies; 7+ messages in thread
From: Neil Jerram @ 2007-11-03 11:12 UTC (permalink / raw)
  To: Dmitry Dzhus; +Cc: guile-user

Dmitry Dzhus <mail@sphinx.net.ru> writes:

> That's terrific, Marco, and that code is almost clear to me.
>
> I'm now strongly convinced that I should introduce myself to Scheme
> macro forms closer. What may a recommended definite guide to it? (I
> read SICP, but that book concentrates on other kind of things).

For define-macro-style macros, this looks useful to me:

http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-10.html#node_chap_8

For syntax-rules, try here:

http://community.schemewiki.org/?syntax-rules

Regards,
        Neil



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Looping local binding
  2007-11-03 10:46 ` Dmitry Dzhus
  2007-11-03 11:12   ` Neil Jerram
@ 2007-11-03 18:33   ` Keith Wright
  1 sibling, 0 replies; 7+ messages in thread
From: Keith Wright @ 2007-11-03 18:33 UTC (permalink / raw)
  To: mail; +Cc: guile-user

> From: Dmitry Dzhus <mail@sphinx.net.ru>
> 
> That's terrific, Marco, and that code is almost clear to me.
> 
> I'm now strongly convinced that I should introduce myself to Scheme
> macro forms closer. What may a recommended definite guide to it? (I
> read SICP, but that book concentrates on other kind of things).

There is no definitive guide.  Macros in Scheme are
one of the more interesting areas of active research.
I do not think there are two implementations that
do it exactly the same way.  The sixth revised report,
which was ratified just weeks ago, has a macro system
that is nothing like that in Guile.

I learned a lot about it by reading the reference
implementation by Andre van Tonder,  Here is a
chunk from my email file.

        Good Luck in your search!

If you only want to know about Guile macros, I'm
afraid you have no choice but to read Guile documentation.

 -- Keith

> From: AndrevanTonder <andre@het.brown.edu>
> Cc: r6rs-discuss@lists.r6rs.org
> 
> > AndrevanTonder wrote:
> >>
> >>      R6RS Libraries and Syntax-case
> >>
> >> implementation is available at:
> >>
> >>     http://www.het.brown.edu/people/andre/macros
> >
> > This is a different library than what Abdulaziz Ghuloum announced recently,
> > <http://www.cs.indiana.edu/~aghuloum/r6rs-libraries/>?
> >
> > What are the differences?
> 
> As you noticed, there are currently two portable reference implementations
> of R6RS libraries and syntax-case.  Even though they are both R6RS compliant,
> there are indeed differences between them.  Aziz may have further comments,
> but the differences that are known to me are the following:
> 
>   * The largest difference is in how identifier import levels are treated.
>     The Van-Tonder implementation enforces declared import levels while the
>     Ghuloum-Dybvig implementation does not.  In other words, the Van-Tonder
>     implementation treats a reference to an identifier outside its declared
>     import levels as a syntax violation, while the Ghuloum-Dybvig
>     implementation ignores import level declarations, allowing references to
>     identifiers outside their declared import levels.  Both models are allowed
>     by R6RS.
> 
>     As a result, a working Van-Tonder library will be portable to the
>     Ghuloum-Dybvig system, but a working Ghuloum-Dybvig library will
>     not necessarily be portable to the Van-Tonder system.

 ...

>     The Van-Tonder implementation is in fact very close to the model
>     developed long ago by Matthew Flatt of PLT, with which there
>     is a large amount of historical experience among the PLT
>     user base.  For more details on the model and the design reasons
>     behind it, see for example the article:
> 
>     Further http://citeseer.ist.psu.edu/flatt02composable.html


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-11-03 18:33 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-29 16:28 Looping local binding Dmitry Dzhus
2007-10-29 22:12 ` Neil Jerram
2007-11-03 10:46   ` Dmitry Dzhus
  -- strict thread matches above, loose matches on Subject: below --
2007-10-30 17:03 Marco Maggi
2007-11-03 10:46 ` Dmitry Dzhus
2007-11-03 11:12   ` Neil Jerram
2007-11-03 18:33   ` Keith Wright

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).