and-let-star.scm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. ;;;; and-let-star.scm --- and-let* syntactic form (draft SRFI-2) for Guile
  2. ;;;; written by Michael Livshin <mike@olan.com>
  3. ;;;;
  4. ;;;; Copyright (C) 1999, 2001, 2004, 2006 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 2.1 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (ice-9 and-let-star)
  20. :export-syntax (and-let*))
  21. (defmacro and-let* (vars . body)
  22. (define (expand vars body)
  23. (cond
  24. ((null? vars)
  25. (if (null? body)
  26. #t
  27. `(begin ,@body)))
  28. ((pair? vars)
  29. (let ((exp (car vars)))
  30. (cond
  31. ((pair? exp)
  32. (cond
  33. ((null? (cdr exp))
  34. `(and ,(car exp) ,(expand (cdr vars) body)))
  35. (else
  36. (let ((var (car exp)))
  37. `(let (,exp)
  38. (and ,var ,(expand (cdr vars) body)))))))
  39. (else
  40. `(and ,exp ,(expand (cdr vars) body))))))
  41. (else
  42. (error "not a proper list" vars))))
  43. (expand vars body))
  44. (cond-expand-provide (current-module) '(srfi-2))