From 8c9734066f46bf86d5067a9ce98194dc39df90c6 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Wed, 29 Jul 2026 23:39:11 +0300 Subject: [PATCH] scm: add if-let/when-let macros Add if-let/if-let*/when-let/when-let* macros for internal use in mu-scm.el. --- scm/mu-scm.scm | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/scm/mu-scm.scm b/scm/mu-scm.scm index 743c7a68..34b9645a 100644 --- a/scm/mu-scm.scm +++ b/scm/mu-scm.scm @@ -126,6 +126,40 @@ ;; ;; Helpers +(define-syntax if-let* + (syntax-rules () + "Bind variables according to VAR and evaluate THEN or ELSE. +Evaluate each binding in turn, as in ‘let*’, stopping if a binding value is nil. +If all are non-nil return the value of THEN, otherwise the value of the last +form in ELSE, or #f if there are none." + ((_ () then) then) + ((_ () then else) then) + ((_ ((var expr) rest ...) then) + (if-let* ((var expr) rest ...) then #f)) + ((_ ((var expr) rest ...) then else) + (let ((var expr)) + (if var + (if-let* (rest ...) then else) + else))))) + +(define-syntax when-let* + (syntax-rules () + "Bind variables according to VAR and evaluate THEN. +Evaluate each binding in turn, as in ‘let*’, stopping if a binding value is nil. +If all are non-nil return the value of THEN, or #f if there are none." + ((_ (bindings ...) body body* ...) + (if-let* (bindings ...) (begin body body* ...))))) + +(define-syntax if-let + (syntax-rules () + "Alias for if-let*." + ((_ args ...) (if-let* args ...)))) + +(define-syntax when-let + (syntax-rules () + "Alias for when-let*." + ((_ args ...) (when-let* args ...)))) + (define (set-documentation! symbol docstring) "Set the docstring for symbol in current module to docstring. This is useful for symbols that do not support docstrings directly, such