Adding let-optionals* to core as recommended way of handling optional arguments.

This is much faster (in chibi) and more concise than case-lambda.  Also adding
(chibi optional) for the let-optionals and opt-lambda variants.
Still need to add let-keywords*.
This commit is contained in:
Alex Shinn 2012-10-21 17:22:16 +09:00
parent 9aa03c0a09
commit cec55dfe41
3 changed files with 34 additions and 0 deletions

15
lib/chibi/optional.scm Normal file
View file

@ -0,0 +1,15 @@
(define-syntax let-optionals
(syntax-rules ()
((let-optionals ("step") ls (vars ...) ((v d) . rest) . body)
(let-optionals ("step") ls (vars ... (v tmp d)) rest . body))
((let-optionals ("step") ls ((var tmp default) ...) rest . body)
(let-optionals* ls ((tmp default) ... . rest)
(let ((var tmp) ...) . body)))
((let-optionals ls vars . body)
(let-optionals ("step") ls () vars . body))))
(define-syntax opt-lambda
(syntax-rules ()
((opt-lambda vars . body)
(lambda args (let-optionals args vars . body)))))

5
lib/chibi/optional.sld Normal file
View file

@ -0,0 +1,5 @@
(define-library (chibi optional)
(export let-optionals let-optionals* opt-lambda)
(import (chibi))
(include "optional.scm"))

View file

@ -861,6 +861,20 @@
((letrec* ((var val) ...) . body) ((letrec* ((var val) ...) . body)
(let () (define var val) ... . body)))) (let () (define var val) ... . body))))
(define-syntax let-optionals*
(syntax-rules ()
((let-optionals* opt-ls () . body)
(begin . body))
((let-optionals* (op . args) vars . body)
(let ((tmp (op . args)))
(let-optionals* tmp vars . body)))
((let-optionals* tmp ((var default) . rest) . body)
(let ((var (if (pair? tmp) (car tmp) default))
(tmp2 (if (pair? tmp) (cdr tmp) '())))
(let-optionals* tmp2 rest . body)))
((let-optionals* tmp tail . body)
(let ((tail tmp)) . body))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; exceptions ;; exceptions