srfi-16.scm 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ; Copyright (C) Lars T Hansen (1999). All Rights Reserved.
  2. ; Permission is hereby granted, free of charge, to any person
  3. ; obtaining a copy of this software and associated documentation files
  4. ; (the "Software"), to deal in the Software without restriction,
  5. ; including without limitation the rights to use, copy, modify, merge,
  6. ; publish, distribute, sublicense, and/or sell copies of the Software,
  7. ; and to permit persons to whom the Software is furnished to do so,
  8. ; subject to the following conditions:
  9. ; The above copyright notice and this permission notice shall be
  10. ; included in all copies or substantial portions of the Software.
  11. ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. ; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. ; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. ; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  15. ; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. ; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. ; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. ; SOFTWARE.
  19. (define-syntax case-lambda
  20. (syntax-rules ()
  21. ((case-lambda
  22. (?a1 ?e1 ...)
  23. ?clause1 ...)
  24. (lambda args
  25. (let ((l (length args)))
  26. (case-lambda "CLAUSE" args l
  27. (?a1 ?e1 ...)
  28. ?clause1 ...))))
  29. ((case-lambda "CLAUSE" ?args ?l
  30. ((?a1 ...) ?e1 ...)
  31. ?clause1 ...)
  32. (if (= ?l (length '(?a1 ...)))
  33. (apply (lambda (?a1 ...) ?e1 ...) ?args)
  34. (case-lambda "CLAUSE" ?args ?l
  35. ?clause1 ...)))
  36. ((case-lambda "CLAUSE" ?args ?l
  37. ((?a1 . ?ar) ?e1 ...)
  38. ?clause1 ...)
  39. (case-lambda "IMPROPER" ?args ?l 1 (?a1 . ?ar) (?ar ?e1 ...)
  40. ?clause1 ...))
  41. ((case-lambda "CLAUSE" ?args ?l
  42. (?a1 ?e1 ...)
  43. ?clause1 ...)
  44. (let ((?a1 ?args))
  45. ?e1 ...))
  46. ((case-lambda "CLAUSE" ?args ?l)
  47. (assertion-violation 'case-lambda "Wrong number of arguments to CASE-LAMBDA."))
  48. ((case-lambda "IMPROPER" ?args ?l ?k ?al ((?a1 . ?ar) ?e1 ...)
  49. ?clause1 ...)
  50. (case-lambda "IMPROPER" ?args ?l (+ ?k 1) ?al (?ar ?e1 ...)
  51. ?clause1 ...))
  52. ((case-lambda "IMPROPER" ?args ?l ?k ?al (?ar ?e1 ...)
  53. ?clause1 ...)
  54. (if (>= ?l ?k)
  55. (apply (lambda ?al ?e1 ...) ?args)
  56. (case-lambda "CLAUSE" ?args ?l
  57. ?clause1 ...)))))