61.sld 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ;;; Copyright (C) 2004 Taylor Campbell. All rights reserved.
  2. ;;; Made an R7RS library by Taylan Ulrich Bayırlı/Kammer, Copyright (C) 2014.
  3. ;;; Permission is hereby granted, free of charge, to any person obtaining a copy
  4. ;;; of this software and associated documentation files (the "Software"), to
  5. ;;; deal in the Software without restriction, including without limitation the
  6. ;;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. ;;; sell copies of the Software, and to permit persons to whom the Software is
  8. ;;; furnished to do so, subject to the following conditions:
  9. ;;; The above copyright notice and this permission notice shall be included in
  10. ;;; all copies or substantial portions of the Software.
  11. ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. ;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. ;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. ;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. ;;; IN THE SOFTWARE.
  18. (define-library (srfi 61)
  19. (export cond)
  20. (import (except (scheme base) cond))
  21. (begin
  22. (define-syntax cond
  23. (syntax-rules (=> else)
  24. ((cond (else else1 else2 ...))
  25. ;; The (if #t (begin ...)) wrapper ensures that there may be no
  26. ;; internal definitions in the body of the clause. R5RS mandates
  27. ;; this in text (by referring to each subform of the clauses as
  28. ;; <expression>) but not in its reference implementation of `cond',
  29. ;; which just expands to (begin ...) with no (if #t ...) wrapper.
  30. (if #t (begin else1 else2 ...)))
  31. ((cond (test => receiver) more-clause ...)
  32. (let ((t test))
  33. (cond/maybe-more t
  34. (receiver t)
  35. more-clause ...)))
  36. ((cond (generator guard => receiver) more-clause ...)
  37. (call-with-values (lambda () generator)
  38. (lambda t
  39. (cond/maybe-more (apply guard t)
  40. (apply receiver t)
  41. more-clause ...))))
  42. ((cond (test) more-clause ...)
  43. (let ((t test))
  44. (cond/maybe-more t t more-clause ...)))
  45. ((cond (test body1 body2 ...) more-clause ...)
  46. (cond/maybe-more test
  47. (begin body1 body2 ...)
  48. more-clause ...))))
  49. (define-syntax cond/maybe-more
  50. (syntax-rules ()
  51. ((cond/maybe-more test consequent)
  52. (if test
  53. consequent))
  54. ((cond/maybe-more test consequent clause ...)
  55. (if test
  56. consequent
  57. (cond clause ...)))))
  58. ))