srfi-17.scm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ;;; srfi-17.scm --- Generalized set!
  2. ;; Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library 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 GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Author: Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de>
  18. ;;; Commentary:
  19. ;; This is an implementation of SRFI-17: Generalized set!
  20. ;;
  21. ;; It exports the Guile procedure `make-procedure-with-setter' under
  22. ;; the SRFI name `getter-with-setter' and exports the standard
  23. ;; procedures `car', `cdr', ..., `cdddr', `string-ref' and
  24. ;; `vector-ref' as procedures with setters, as required by the SRFI.
  25. ;;
  26. ;; SRFI-17 was heavily criticized during its discussion period but it
  27. ;; was finalized anyway. One issue was its concept of globally
  28. ;; associating setter "properties" with (procedure) values, which is
  29. ;; non-Schemy. For this reason, this implementation chooses not to
  30. ;; provide a way to set the setter of a procedure. In fact, (set!
  31. ;; (setter PROC) SETTER) signals an error. The only way to attach a
  32. ;; setter to a procedure is to create a new object (a "procedure with
  33. ;; setter") via the `getter-with-setter' procedure. This procedure is
  34. ;; also specified in the SRFI. Using it avoids the described
  35. ;; problems.
  36. ;;
  37. ;; This module is fully documented in the Guile Reference Manual.
  38. ;;; Code:
  39. (define-module (srfi srfi-17)
  40. :export (getter-with-setter)
  41. :replace (;; redefined standard procedures
  42. setter
  43. car cdr caar cadr cdar cddr caaar caadr cadar caddr cdaar
  44. cdadr cddar cdddr caaaar caaadr caadar caaddr cadaar cadadr
  45. caddar cadddr cdaaar cdaadr cdadar cdaddr cddaar cddadr
  46. cdddar cddddr string-ref vector-ref))
  47. (cond-expand-provide (current-module) '(srfi-17))
  48. ;;; Procedures
  49. (define getter-with-setter make-procedure-with-setter)
  50. (define setter
  51. (getter-with-setter
  52. (@ (guile) setter)
  53. (lambda args
  54. (error "Setting setters is not supported for a good reason."))))
  55. ;;; Redefine R5RS procedures to appropriate procedures with setters
  56. (define (compose-setter setter location)
  57. (lambda (obj value)
  58. (setter (location obj) value)))
  59. (define car
  60. (getter-with-setter (@ (guile) car)
  61. set-car!))
  62. (define cdr
  63. (getter-with-setter (@ (guile) cdr)
  64. set-cdr!))
  65. (define caar
  66. (getter-with-setter (@ (guile) caar)
  67. (compose-setter set-car! (@ (guile) car))))
  68. (define cadr
  69. (getter-with-setter (@ (guile) cadr)
  70. (compose-setter set-car! (@ (guile) cdr))))
  71. (define cdar
  72. (getter-with-setter (@ (guile) cdar)
  73. (compose-setter set-cdr! (@ (guile) car))))
  74. (define cddr
  75. (getter-with-setter (@ (guile) cddr)
  76. (compose-setter set-cdr! (@ (guile) cdr))))
  77. (define caaar
  78. (getter-with-setter (@ (guile) caaar)
  79. (compose-setter set-car! (@ (guile) caar))))
  80. (define caadr
  81. (getter-with-setter (@ (guile) caadr)
  82. (compose-setter set-car! (@ (guile) cadr))))
  83. (define cadar
  84. (getter-with-setter (@ (guile) cadar)
  85. (compose-setter set-car! (@ (guile) cdar))))
  86. (define caddr
  87. (getter-with-setter (@ (guile) caddr)
  88. (compose-setter set-car! (@ (guile) cddr))))
  89. (define cdaar
  90. (getter-with-setter (@ (guile) cdaar)
  91. (compose-setter set-cdr! (@ (guile) caar))))
  92. (define cdadr
  93. (getter-with-setter (@ (guile) cdadr)
  94. (compose-setter set-cdr! (@ (guile) cadr))))
  95. (define cddar
  96. (getter-with-setter (@ (guile) cddar)
  97. (compose-setter set-cdr! (@ (guile) cdar))))
  98. (define cdddr
  99. (getter-with-setter (@ (guile) cdddr)
  100. (compose-setter set-cdr! (@ (guile) cddr))))
  101. (define caaaar
  102. (getter-with-setter (@ (guile) caaaar)
  103. (compose-setter set-car! (@ (guile) caaar))))
  104. (define caaadr
  105. (getter-with-setter (@ (guile) caaadr)
  106. (compose-setter set-car! (@ (guile) caadr))))
  107. (define caadar
  108. (getter-with-setter (@ (guile) caadar)
  109. (compose-setter set-car! (@ (guile) cadar))))
  110. (define caaddr
  111. (getter-with-setter (@ (guile) caaddr)
  112. (compose-setter set-car! (@ (guile) caddr))))
  113. (define cadaar
  114. (getter-with-setter (@ (guile) cadaar)
  115. (compose-setter set-car! (@ (guile) cdaar))))
  116. (define cadadr
  117. (getter-with-setter (@ (guile) cadadr)
  118. (compose-setter set-car! (@ (guile) cdadr))))
  119. (define caddar
  120. (getter-with-setter (@ (guile) caddar)
  121. (compose-setter set-car! (@ (guile) cddar))))
  122. (define cadddr
  123. (getter-with-setter (@ (guile) cadddr)
  124. (compose-setter set-car! (@ (guile) cdddr))))
  125. (define cdaaar
  126. (getter-with-setter (@ (guile) cdaaar)
  127. (compose-setter set-cdr! (@ (guile) caaar))))
  128. (define cdaadr
  129. (getter-with-setter (@ (guile) cdaadr)
  130. (compose-setter set-cdr! (@ (guile) caadr))))
  131. (define cdadar
  132. (getter-with-setter (@ (guile) cdadar)
  133. (compose-setter set-cdr! (@ (guile) cadar))))
  134. (define cdaddr
  135. (getter-with-setter (@ (guile) cdaddr)
  136. (compose-setter set-cdr! (@ (guile) caddr))))
  137. (define cddaar
  138. (getter-with-setter (@ (guile) cddaar)
  139. (compose-setter set-cdr! (@ (guile) cdaar))))
  140. (define cddadr
  141. (getter-with-setter (@ (guile) cddadr)
  142. (compose-setter set-cdr! (@ (guile) cdadr))))
  143. (define cdddar
  144. (getter-with-setter (@ (guile) cdddar)
  145. (compose-setter set-cdr! (@ (guile) cddar))))
  146. (define cddddr
  147. (getter-with-setter (@ (guile) cddddr)
  148. (compose-setter set-cdr! (@ (guile) cdddr))))
  149. (define string-ref
  150. (getter-with-setter (@ (guile) string-ref)
  151. string-set!))
  152. (define vector-ref
  153. (getter-with-setter (@ (guile) vector-ref)
  154. vector-set!))
  155. ;;; srfi-17.scm ends here