SRFI-227 Optional Arguments
Description
This SRFI provides syntax forms for simplifying working with functions with optional arguments, including an extended version of the let-optionals and let-optionals* that's part of (chicken base).
Author
Shawn Wagner
Repository
https://github.com/shawnw/chicken-srfi-227
API
Taken from the reference documentation.
(srfi 227) library
[syntax] (opt-lambda opt-formals body)Syntax: Opt-formals is either of the form (variable1 … variablen binding1 … bindingm) or (variable1 … variablen binding1 … bindingm . variable), where each bindingi has the form (variablen + i init), where each init is an expression. It is a syntax violation if the same variable appears more than once among the variables.
Semantics: An opt-lambda expression evaluates to a procedure and is lexically scoped in the same manner as a procedure resulting from a lambda expression. When the procedure is later called with actual arguments, the variables are bound to fresh locations, the values of the corresponding arguments are stored in those locations, the body is evaluated in the extended environment, and the results of body are returned as the results of the procedure call.
A procedure created with the first syntax of opt-formals takes at least n arguments and at most n + m arguments. A procedure created with the second syntax of opt-formals takes n or more arguments. If the procedure is called with fewer than n + m (but at least n arguments), the missing actual arguments are substituted by the values resulting from evaluating the corresponding inits. The corresponding inits are evaluated in an unspecified order in the lexical environment of the opt-lambda expression when the procedure is called.
It is an assertion violation if the procedure created with the first syntax of opt-formals is called with more than n + m actual arguments. The value stored in the binding of variable of a procedure created with the second syntax of opt-formals will be a newly allocated list of the actual arguments left over after all the other actual arguments have been matched up against the other formal arguments (or the empty list in case no actual arguments are left over).
Note: Both n and m may be zero.
[syntax] (opt*-lambda opt-formals body)Similar to opt-lambda except that the inits corresponding to missing actual arguments are evaluated sequentially from left to right, and the region of the binding of a variable is that part of the opt*-lambda expression to the right of it or its binding.
[syntax] (let-optionals expression opt-formals body)Syntax: Opt-formals is as in a opt-lambda expression.
Semantics: Operationally equivalent to (apply (opt-lambda opt-formals body) expression)
[syntax] (let-optionals* expression opt-formals body)Similar to let-optionals except that opt-lambda is replaced with opt*-lambda in the operational definition.
(srfi 227 definition) library
[syntax] (define-optionals (identifier . opt-formals) body)Syntax: Opt-formals is as in a opt-lambda expression.
Semantics: Operationally equivalent to (define identifier (opt-lambda opt-formals body))
[syntax] (define-optionals* (identifier . opt-formals) body)Syntax: Opt-formals is as in a opt-lambda expression.
Semantics: Operationally equivalent to (define identifier (opt*-lambda opt-formals body))
License
Copyright © 2023 Shawn Wagner <shawnw.mobile@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Version History
- 1.0 - initial release