Skip to content

Instantly share code, notes, and snippets.

@SaitoAtsushi
Created February 17, 2011 07:56
Show Gist options
  • Save SaitoAtsushi/831264 to your computer and use it in GitHub Desktop.
Save SaitoAtsushi/831264 to your computer and use it in GitHub Desktop.
(define-syntax let-optionals
(syntax-rules ()
((_ args ((var default) rest ... . restarg) body ...)
(let* ((tmp args)
(var (if (null? tmp) default (car tmp))))
(let-optionals (if (null? tmp) '() (cdr tmp))
(rest ... . restarg)
body ...)))
((_ args () body ...)
(begin body ...))
((_ args restarg body ...)
(let ((restarg args))
body ...))))
(define-syntax let-optionals*
(lambda(stx)
(define (add-default spec)
(syntax-case spec ()
((var default) #'(var default))
(var #'(var #f))))
(syntax-case stx ()
((_ args (spec ... . restarg) body0 body1 ...)
#`(let-optionals args (#,@(map add-default #'(spec ...)) . restarg)
body0 body1 ...)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment