vararg.scm 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Mike Sperber
  3. ; Library functionality for writing procedures with variable number of arguments.
  4. ; This has the same interface as the OPT-LAMBDA in PLT Scheme's etc.ss
  5. ; library.
  6. (define-syntax opt-lambda
  7. (syntax-rules ()
  8. ((opt-lambda (?clause1 . ?clauses) ?body1 ?body ...)
  9. (opt-lambda-aux-1 (?clause1 . ?clauses) () ?body1 ?body ...))
  10. ((opt-lambda ?id ?body1 ?body ...)
  11. (lambda ?id ?body1 ?body ...))))
  12. ; process the initial vanilla parameters
  13. (define-syntax opt-lambda-aux-1
  14. (syntax-rules ()
  15. ((opt-lambda-aux-1 () (?arg ...) ?body ...)
  16. (lambda (?arg ...) ?body ...))
  17. ((opt-lambda-aux-1 ((?id ?default) . ?rest) (?arg ...) ?body ...)
  18. (opt-lambda-aux-2 ((?id ?default) . ?rest)
  19. (?arg ... . rest) rest ()
  20. ?body ...))
  21. ((opt-lambda-aux-1 (?id . ?rest) (?arg ...) ?body ...)
  22. (opt-lambda-aux-1 ?rest (?arg ... ?id) ?body ...))))
  23. ; this processes from the optionals on
  24. (define-syntax opt-lambda-aux-2
  25. (syntax-rules ()
  26. ((opt-lambda-aux-2 () ?args ?rest-param (?lclause ...) ?body ...)
  27. (lambda ?args
  28. (let* (?lclause ...)
  29. ?body ...)))
  30. ;; optimization
  31. ((opt-lambda-aux-2 ((?id ?default))
  32. ?args ?rest-param (?lclause ...) ?body ...)
  33. (lambda ?args
  34. (let* (?lclause
  35. ...
  36. (?id (if (pair? ?rest-param)
  37. (car ?rest-param)
  38. ?default)))
  39. ?body ...)))
  40. ((opt-lambda-aux-2 ((?id ?default) ?rest ...)
  41. ?args ?rest-param (?lclause ...) ?body ...)
  42. (opt-lambda-aux-2 (?rest ...)
  43. ?args
  44. new-rest
  45. (?lclause ...
  46. (?id (if (pair? ?rest-param)
  47. (car ?rest-param)
  48. ?default))
  49. (new-rest (if (pair? ?rest-param)
  50. (cdr ?rest-param)
  51. '())))
  52. ?body ...))
  53. ;; kludge for dealing with rest parameter
  54. ((opt-lambda-aux-2 ((?id ?default) . (?rest1 . ?rest))
  55. ?args ?rest-param (?lclause ...) ?body ...)
  56. (opt-lambda-aux-2 (?rest1 . ?rest)
  57. ?args
  58. new-rest
  59. (?lclause ...
  60. (?id (if (pair? ?rest-param)
  61. (car ?rest-param)
  62. ?default))
  63. (new-rest (if (pair? ?rest-param)
  64. (cdr ?rest-param)
  65. '())))
  66. ?body ...))
  67. ((opt-lambda-aux-2 ((?id ?default) . ?rest)
  68. ?args ?rest-param (?lclause ...) ?body ...)
  69. (lambda ?args
  70. (let* (?lclause
  71. ...
  72. (?id (if (pair? ?rest-param)
  73. (car ?rest-param)
  74. ?default))
  75. (?rest (if (pair? ?rest-param)
  76. (cdr ?rest-param)
  77. '())))
  78. ?body ...)))))