vararg.scm 2.5 KB

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