srfi-16.scm 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. ; Copied from Lars T Hansen's SRFI document.
  2. (define-syntax case-lambda
  3. (syntax-rules ()
  4. ((case-lambda
  5. (?a1 ?e1 ...)
  6. ?clause1 ...)
  7. (lambda args
  8. (let ((l (length args)))
  9. (case-lambda "CLAUSE" args l
  10. (?a1 ?e1 ...)
  11. ?clause1 ...))))
  12. ((case-lambda "CLAUSE" ?args ?l
  13. ((?a1 ...) ?e1 ...)
  14. ?clause1 ...)
  15. (if (= ?l (length '(?a1 ...)))
  16. (apply (lambda (?a1 ...) ?e1 ...) ?args)
  17. (case-lambda "CLAUSE" ?args ?l
  18. ?clause1 ...)))
  19. ((case-lambda "CLAUSE" ?args ?l
  20. ((?a1 . ?ar) ?e1 ...)
  21. ?clause1 ...)
  22. (case-lambda "IMPROPER" ?args ?l 1 (?a1 . ?ar) (?ar ?e1 ...)
  23. ?clause1 ...))
  24. ((case-lambda "CLAUSE" ?args ?l
  25. (?a1 ?e1 ...)
  26. ?clause1 ...)
  27. (let ((?a1 ?args))
  28. ?e1 ...))
  29. ((case-lambda "CLAUSE" ?args ?l)
  30. (error "Wrong number of arguments to CASE-LAMBDA."))
  31. ((case-lambda "IMPROPER" ?args ?l ?k ?al ((?a1 . ?ar) ?e1 ...)
  32. ?clause1 ...)
  33. (case-lambda "IMPROPER" ?args ?l (+ ?k 1) ?al (?ar ?e1 ...)
  34. ?clause1 ...))
  35. ((case-lambda "IMPROPER" ?args ?l ?k ?al (?ar ?e1 ...)
  36. ?clause1 ...)
  37. (if (>= ?l ?k)
  38. (apply (lambda ?al ?e1 ...) ?args)
  39. (case-lambda "CLAUSE" ?args ?l
  40. ?clause1 ...)))))