unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* let-keywords?
@ 2006-12-11 15:37 Han-Wen Nienhuys
  2006-12-11 23:35 ` let-keywords? Kevin Ryde
  0 siblings, 1 reply; 5+ messages in thread
From: Han-Wen Nienhuys @ 2006-12-11 15:37 UTC (permalink / raw)



Hi,


I think that ice-9's let-keyword* macro is the thing that I've been
looking for, but I haven't been able to work out how to use it. 

Could we have an example in the manual? 
 
Thanks!  

-- 
 Han-Wen Nienhuys - hanwen@xs4all.nl - http://www.xs4all.nl/~hanwen



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


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

* Re: let-keywords?
  2006-12-11 15:37 let-keywords? Han-Wen Nienhuys
@ 2006-12-11 23:35 ` Kevin Ryde
  2006-12-11 23:57   ` let-keywords? Han-Wen Nienhuys
  0 siblings, 1 reply; 5+ messages in thread
From: Kevin Ryde @ 2006-12-11 23:35 UTC (permalink / raw)
  Cc: guile-devel

Han-Wen Nienhuys <hanwen@xs4all.nl> writes:
>
> I think that ice-9's let-keyword* macro is the thing that I've been
> looking for, but I haven't been able to work out how to use it. 

I think it goes like

	(let-keywords '(#:foo "hello" #:xyzzy "world") #t
	              ((foo  "default for foo")
	               (bar  "default for bar"))
	  foo => "hello"
	  bar => "default for bar"

"allow-other-keywords" is whether it's an error to have something
unknown like #:xyzzy in the args.

I think the "let-keywords*" variant allows the default for one
variable to use the value of a previous one.

	(let-keywords* '(#:foo 1 #:xyzzy 2) #t
	               ((foo  1)
	                (bar  (+ foo 2)))
	  foo => 1
	  bar => 3

Would those help for the manual, or can anyone think of more typical
or clearer samples?


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


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

* Re: let-keywords?
  2006-12-11 23:35 ` let-keywords? Kevin Ryde
@ 2006-12-11 23:57   ` Han-Wen Nienhuys
  2006-12-13 21:28     ` let-keywords? Kevin Ryde
  0 siblings, 1 reply; 5+ messages in thread
From: Han-Wen Nienhuys @ 2006-12-11 23:57 UTC (permalink / raw)
  Cc: guile-devel

Kevin Ryde escreveu:

> I think the "let-keywords*" variant allows the default for one
> variable to use the value of a previous one.
> 
> 	(let-keywords* '(#:foo 1 #:xyzzy 2) #t
> 	               ((foo  1)
> 	                (bar  (+ foo 2)))
> 	  foo => 1
> 	  bar => 3
> 
> Would those help for the manual, or can anyone think of more typical

certainly. Just something mentioning #:foo would be an enormous help
(I was blindly trying 'foo instead)

-- 
 Han-Wen Nienhuys - hanwen@xs4all.nl - http://www.xs4all.nl/~hanwen


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


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

* Re: let-keywords?
  2006-12-11 23:57   ` let-keywords? Han-Wen Nienhuys
@ 2006-12-13 21:28     ` Kevin Ryde
  2006-12-14 11:20       ` let-keywords? Han-Wen Nienhuys
  0 siblings, 1 reply; 5+ messages in thread
From: Kevin Ryde @ 2006-12-13 21:28 UTC (permalink / raw)
  Cc: guile-devel

I checked in the text below.



5.8.3.2 let-keywords Reference
..............................

`let-keywords' and `let-keywords*' extract values from keyword style
argument lists, binding local variables to those values or to defaults.

 -- library syntax: let-keywords args allow-other-keys? (binding ...)
          body ...
 -- library syntax: let-keywords* args allow-other-keys? (binding ...)
          body ...
     ARGS is evaluated and should give a list of the form `(#:keyword1
     value1 #:keyword2 value2 ...)'.  The BINDINGs are variables and
     default expressions, with the variables to be set (by name) from
     the keyword values.  The BODY forms are then evaluated and the
     last is the result.  An example will make the syntax clearest,

          (define args '(#:xyzzy "hello" #:foo "world"))

          (let-keywords args #t
                ((foo  "default for foo")
                 (bar  (string-append "default" "for" "bar")))
            (display foo)
            (display ", ")
            (display bar))
          -| world, defaultforbar

     The binding for `foo' comes from the `#:foo' keyword in `args'.
     But the binding for `bar' is the default in the `let-keywords',
     since there's no `#:bar' in the args.

     ALLOW-OTHER-KEYS? is evaluated and controls whether unknown
     keywords are allowed in the ARGS list.  When true other keys are
     ignored (such as `#:xyzzy' in the example), when `#f' an error is
     thrown for anything unknown.

     `let-keywords' is like `let' (*note Local Bindings::) in that all
     bindings are made at once, the defaults expressions are evaluated
     (if needed) outside the scope of the `let-keywords'.

     `let-keywords*' is like `let*', each binding is made successively,
     and the default expressions see the bindings previously made.
     This is the style used by `lambda*' keywords (*note lambda*
     Reference::).  For example,

          (define args '(#:foo 3))

          (let-keywords* args #f
                ((foo  99)
                 (bar  (+ foo 6)))
            (display bar))
          -| 9

     The expression for each default is only evaluated if it's needed,
     ie. if the keyword doesn't appear in ARGS.  So one way to make a
     keyword mandatory is to throw an error of some sort as the default.

          (define args '(#:start 7 #:finish 13))

          (let-keywords* args #t
                ((start 0)
                 (stop  (error "missing #:stop argument")))
            (display bar))
          => ERROR: missing #:stop argument


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


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

* Re: let-keywords?
  2006-12-13 21:28     ` let-keywords? Kevin Ryde
@ 2006-12-14 11:20       ` Han-Wen Nienhuys
  0 siblings, 0 replies; 5+ messages in thread
From: Han-Wen Nienhuys @ 2006-12-14 11:20 UTC (permalink / raw)


Kevin Ryde escreveu:
> I checked in the text below.
> 
> 
> 
> 5.8.3.2 let-keywords Reference
> ..............................
> 
> `let-keywords' and `let-keywords*' extract values from keyword style
> argument lists, binding local variables to those values or to defaults.
> 
>  -- library syntax: let-keywords args allow-other-keys? (binding ...)
>           body ...
>  -- library syntax: let-keywords* args allow-other-keys? (binding ...)
>           body ...
>      ARGS is evaluated and should give a list of the form `(#:keyword1
>      value1 #:keyword2 value2 ...)'.  The BINDINGs are variables and
>      default expressions, with the variables to be set (by name) from
>      the keyword values.  The BODY forms are then evaluated and the
>      last is the result.  An example will make the syntax clearest,


excellent!

-- 
 Han-Wen Nienhuys - hanwen@xs4all.nl - http://www.xs4all.nl/~hanwen



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


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

end of thread, other threads:[~2006-12-14 11:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-12-11 15:37 let-keywords? Han-Wen Nienhuys
2006-12-11 23:35 ` let-keywords? Kevin Ryde
2006-12-11 23:57   ` let-keywords? Han-Wen Nienhuys
2006-12-13 21:28     ` let-keywords? Kevin Ryde
2006-12-14 11:20       ` let-keywords? Han-Wen Nienhuys

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).