iterat.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;;; Structure iterators
  21. (declare (usual-integrations))
  22. ;;; Structural Lists
  23. #|
  24. (define (generate-list n proc) ; ==> ( (proc 0) (proc 1) ... (proc n-1) )
  25. (let loop ((i (fix:- n 1)) (list '()))
  26. (if (fix:< i 0)
  27. list
  28. (loop (fix:- i 1) (cons (proc i) list)))))
  29. |#
  30. (define generate-list make-initialized-list)
  31. (define list:generate make-initialized-list)
  32. (define (list-with-substituted-coord lst i x)
  33. (append (list-head lst i)
  34. (list x)
  35. (cdr (list-tail lst i))))
  36. ;;; Structural Vectors
  37. #|
  38. ;;; Scheme supplies
  39. vector-ref
  40. vector-set!
  41. vector
  42. make-initialized-vector
  43. (define (generate-vector size proc)
  44. (let ((ans (make-vector size)))
  45. (let loop ((i 0))
  46. (if (fix:= i size)
  47. ans
  48. (begin (vector-set! ans i (proc i))
  49. (loop (fix:+ i 1)))))))
  50. |#
  51. (define generate-vector make-initialized-vector)
  52. (define* ((vector-elementwise f) . vectors)
  53. (make-initialized-vector
  54. (vector-length (car vectors))
  55. (lambda (i)
  56. (apply f
  57. (map (lambda (v) (vector-ref v i))
  58. vectors)))))
  59. (define (vector-forall p? . vectors)
  60. (let lp ((i (fix:- (vector-length (car vectors)) 1)))
  61. (cond ((fix:= i 0)
  62. (apply p? (map (lambda (v) (vector-ref v i))
  63. vectors)))
  64. ((apply p? (map (lambda (v) (vector-ref v i))
  65. vectors))
  66. (lp (fix:- i 1)))
  67. (else #f))))
  68. (define (vector-exists p? . vectors)
  69. (let lp ((i (fix:- (vector-length (car vectors)) 1)))
  70. (cond ((fix:= i 0)
  71. (apply p? (map (lambda (v) (vector-ref v i))
  72. vectors)))
  73. ((apply p? (map (lambda (v) (vector-ref v i))
  74. vectors))
  75. #t)
  76. (else
  77. (lp (fix:- i 1))))))
  78. (define (vector-accumulate acc fun init v)
  79. (let ((l (vector-length v)))
  80. (if (fix:= l 0)
  81. init
  82. (let loop ((i 1)
  83. (ans (fun (vector-ref v 0))))
  84. (if (fix:= i l)
  85. ans
  86. (loop (fix:+ i 1)
  87. (acc ans (fun (vector-ref v i)))))))))
  88. (define (vector-with-substituted-coord v i x)
  89. (make-initialized-vector (vector-length v)
  90. (lambda (j)
  91. (if (fix:= j i)
  92. x
  93. (vector-ref v j)))))
  94. ;;; Structural 2-dimensional arrays
  95. ;;; Structrure procedures -- operate on raw array material
  96. (define (array-ref m i j)
  97. (vector-ref (vector-ref m i) j))
  98. (define (array-set! m i j v)
  99. (vector-set! (vector-ref m i) j v))
  100. (define (generate-array rows cols proc)
  101. (make-initialized-vector
  102. rows
  103. (lambda (row)
  104. (make-initialized-vector
  105. cols
  106. (lambda (col)
  107. (proc row col))))))
  108. (define* ((array-elementwise f) . arrays)
  109. (generate-array
  110. (num-rows (car arrays))
  111. (num-cols (car arrays))
  112. (lambda (i j)
  113. (apply f
  114. (map (lambda (m)
  115. (array-ref m i j))
  116. arrays)))))
  117. (define (array-copy m)
  118. (generate-array (num-rows m) (num-cols m)
  119. (lambda (i j) (array-ref m i j))))
  120. (define (num-rows array)
  121. (vector-length array))
  122. (define (num-cols array)
  123. (vector-length (vector-ref array 0)))
  124. (define (nth-row M n) ;return as a vector
  125. (vector-ref M n))
  126. (define (nth-col M j)
  127. (generate-vector (num-rows M)
  128. (lambda (i)
  129. (array-ref M i j))))
  130. (define (array-with-substituted-row A i V)
  131. (vector-with-substituted-coord A i V))
  132. (define (array-with-substituted-col A k V)
  133. (generate-array (num-rows A) (num-cols A)
  134. (lambda (i j)
  135. (if (fix:= j k)
  136. (vector-ref V i)
  137. (array-ref A i j)))))
  138. (define (array-by-rows M)
  139. (apply vector (map list->vector M)))
  140. (define (array-by-cols M)
  141. (transpose-array (array-by-rows M)))
  142. (define (transpose-array M)
  143. (generate-array (num-cols M) (num-rows M)
  144. (lambda (i j) (array-ref M j i))))