[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Scheme-reports] practical matters - CSAN
| Date: Mon, 29 Aug 2011 12:59:15 -0400
| From: John Cowan <cowan@x>
| Aubrey Jaffer scripsit:
| > LET-VALUES, LET*-VALUES, and (srfi-8) RECEIVE expect the number
| > of bindings to match the number of return values. They can't be
| > used if the number of return values is not known in advance.
| In fact they can:
| (let-value (let (((x y . z) (some-mv-form))) <body>)
| will bind the first two values of (some-mv-form) to x and y, and any
| remaining values as a list to z.
Does r7rs-draft-3 allow this? Even if it does, it is an error in your
example if fewer than 2 values are returned.
Scanf is used for reading non-uniform data. Here is a snippet from
Procedures like this are tried in sequence until one matches the line
(case (sscanf line " %24[a-zA-Z0-9_ ] %d %d %d %e %e %e %s"
name r g b ri gi bi junk)
(set! method-id 'm7)
(list (check-match line (color:sRGB r g b) (floats->rgb ri gi bi))
The variables NAME R, G, B, RI, GI, BI, and JUNK are bound in the
containing procedure. When there is no match, sscanf returns 0; for
partial matches sscanf returns between 1 and 6; reading 8 items means
that there was extra stuff in line.
| > CASE-LAMBDA can be used if the desired behavior changes radically with
| > different numbers of values; but often there is a lot of commonality
| > in the procedure. A shared internal definition can't be called
| > because CASE-LAMBDA is the outer binding of the definition. So either
| > code must be replicated in each clause or each clause must call an
| > external procedure with all the arguments.
| A case-lambda can tail-call itself:
| (define x (case-lambda
| ((a b) ...)
| ((a) (x a 0))))
| A compiler can detect this case and optimize it. In addition, the
| case-lambda can be wrapped in a letrec containing internal definitions
| visible to the case-lambda.
The example use of scanf above treats its first matched item quite
differently from subsequent items. The recursive CASE-LAMBDA is no
help to this application.
Scheme-reports mailing list