Skip to content

Instantly share code, notes, and snippets.

@edw
Created April 27, 2020 15:18
Show Gist options
  • Save edw/dfd03d7ccd3f65a9056ff84a40f170cf to your computer and use it in GitHub Desktop.
Save edw/dfd03d7ccd3f65a9056ff84a40f170cf to your computer and use it in GitHub Desktop.
Scheme `let-optionally`
(define-syntax let-optionally
(syntax-rules (_)
((_ vs () e f ...)
(let () e f ...))
((_ vs (_ xv ...) e f ...)
(if (null? vs)
(let-optionally '() (xv ...) e f ...)
(let-optionally (cdr vs) (xv ...) e f ...)))
((_ vs ((x v) xv ...) e f ...)
(if (null? vs)
(let ((x v))
(let-optionally '() (xv ...) e f ...))
(let ((x (car vs)))
(let-optionally (cdr vs) (xv ...) e f ...))))))
(import (chibi test))
(test-begin "LET-OPTIONALLY")
(test '(0 1 3 4)
(let-optionally '(0 1 2 3 4 5)
((x #f) (y #f) _ (z #f) (w #f) _ _ _)
(list x y z w)))
(test-end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment