5.sld 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ;;; 5.sld --- SRFI-5: A compatible let form with signatures and rest arguments
  2. ;; Copyright (C) 2014 Taylan Ulrich Bayırlı/Kammer
  3. ;; Author: Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
  4. ;; Keywords: srfi-5 srfi 5 let
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU Lesser General Public License as published
  7. ;; by the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU Lesser General Public License for more details.
  13. ;; You should have received a copy of the GNU Lesser General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Please don't use weird non-standard licenses for your works! Thankfully this
  17. ;; SRFI is trivial to implement, because the license of the reference
  18. ;; implementation made me irk.
  19. ;;; Code:
  20. (define-library (srfi 5)
  21. (export (rename let+ let))
  22. (import (scheme base))
  23. (begin
  24. (define-syntax let+
  25. (syntax-rules ()
  26. ;; Unnamed, no rest args.
  27. ((_ ((var val) ...) body ...)
  28. (let ((var val) ...) body ...))
  29. ;; Unnamed, with rest args.
  30. ((_ ((var val) spec ...) body ...)
  31. (rest ((var val) spec ...) () () body ...))
  32. ;; Signature style, no rest args.
  33. ((_ (name (var val) ...) body ...)
  34. (let name ((var val) ...) body ...))
  35. ;; Signature style, with rest args.
  36. ((_ (name (var val) spec ...) body ...)
  37. (rest/named name ((var val) spec ...) () () body ...))
  38. ;; Named let, no rest args.
  39. ((_ name ((var val) ...) body ...)
  40. (let name ((var val) ...) body ...))
  41. ;; Named let, with rest args.
  42. ((_ name ((var val) spec ...) body ...)
  43. (rest/named name ((var val) spec ...) () () body ...))))
  44. (define-syntax rest
  45. (syntax-rules ()
  46. ((_ ((var val) spec ...) (var* ...) (val* ...) body ...)
  47. (rest name (spec ...) (var var* ...) (val val* ...) body ...))
  48. ((_ (rest-var rest-val ...) (var ...) (val ...) body ...)
  49. (let ((var val)
  50. ...
  51. (rest-var (list rest-val ...)))
  52. body ...))))
  53. (define-syntax rest/named
  54. (syntax-rules ()
  55. ((_ name ((var val) spec ...) (var* ...) (val* ...) body ...)
  56. (rest/named name (spec ...) (var var* ...) (val val* ...) body ...))
  57. ((_ name (rest-var rest-val ...) (var ...) (val ...) body ...)
  58. (letrec ((name (lambda (var ... . rest-var) body ...)))
  59. (name val ... rest-val ...)))))
  60. ))
  61. ;;; 5.sld ends here