list.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Mike Sperber
  3. ; The (rnrs lists (6)) library.
  4. (define (assert-procedure who obj)
  5. (if (not (procedure? obj))
  6. (assertion-violation who "not a procedure" obj)))
  7. (define (find proc list)
  8. (assert-procedure 'find proc)
  9. (let loop ((list list))
  10. (cond
  11. ((null? list) #f)
  12. ((proc (car list)) (car list))
  13. (else (loop (cdr list))))))
  14. (define (check-nulls who the-list the-lists lists)
  15. (for-each (lambda (list)
  16. (if (not (null? list))
  17. (apply assertion-violation who
  18. "argument lists don't have the same size"
  19. list lists)))
  20. lists))
  21. (define (for-all proc list . lists)
  22. (assert-procedure 'for-all proc)
  23. (cond
  24. ((null? lists)
  25. (for-all1 proc list))
  26. ((null? list)
  27. (check-nulls 'for-all list lists lists)
  28. #t)
  29. (else
  30. (let loop ((list list) (lists lists))
  31. (let ((next (cdr list)))
  32. (cond
  33. ((null? next)
  34. (apply proc (car list) (map car lists)))
  35. ((apply proc (car list) (map car lists))
  36. (loop next (map cdr lists)))
  37. (else #f)))))))
  38. (define (for-all1 proc list)
  39. (if (null? list)
  40. #t
  41. (let loop ((list list))
  42. (let ((next (cdr list)))
  43. (cond
  44. ((null? next) (proc (car list)))
  45. ((proc (car list)) (loop next))
  46. (else #f))))))
  47. (define (exists proc list . lists)
  48. (assert-procedure 'exists proc)
  49. (cond
  50. ((null? lists)
  51. (exists1 proc list))
  52. ((null? list)
  53. (check-nulls 'exists list lists lists)
  54. #f)
  55. (else
  56. (let loop ((list list) (lists lists))
  57. (let ((next (cdr list)))
  58. (if (null? next)
  59. (apply proc (car list) (map car lists))
  60. (or (apply proc (car list) (map car lists))
  61. (loop next (map cdr lists)))))))))
  62. (define (exists1 proc list)
  63. (if (null? list)
  64. #f
  65. (let loop ((list list))
  66. (let ((next (cdr list)))
  67. (if (null? next)
  68. (proc (car list))
  69. (or (proc (car list))
  70. (loop next)))))))
  71. (define (filter proc list)
  72. (assert-procedure 'filter proc)
  73. (let loop ((list list) (r '()))
  74. (cond ((null? list)
  75. (reverse r))
  76. ((proc (car list))
  77. (loop (cdr list) (cons (car list) r)))
  78. (else
  79. (loop (cdr list) r)))))
  80. (define (partition proc list)
  81. (assert-procedure 'partition proc)
  82. (let loop ((list list) (yes '()) (no '()))
  83. (cond ((null? list)
  84. (values (reverse yes) (reverse no)))
  85. ((proc (car list))
  86. (loop (cdr list) (cons (car list) yes) no))
  87. (else
  88. (loop (cdr list) yes (cons (car list) no))))))
  89. (define (fold-left combine nil the-list . the-lists)
  90. (assert-procedure 'fold-left combine)
  91. (if (null? the-lists)
  92. (fold-left1 combine nil the-list)
  93. (let loop ((accum nil) (list the-list) (lists the-lists))
  94. (if (null? list)
  95. (begin
  96. (check-nulls 'fold-left the-list the-lists lists)
  97. accum)
  98. (loop (apply combine accum (car list) (map car lists))
  99. (cdr list)
  100. (map cdr lists))))))
  101. (define (fold-left1 combine nil list)
  102. (let loop ((accum nil) (list list))
  103. (if (null? list)
  104. accum
  105. (loop (combine accum (car list))
  106. (cdr list)))))
  107. (define (fold-right combine nil the-list . the-lists)
  108. (assert-procedure 'fold-right combine)
  109. (if (null? the-lists)
  110. (fold-right1 combine nil the-list)
  111. (let recur ((list the-list) (lists the-lists))
  112. (if (null? list)
  113. (begin
  114. (check-nulls 'fold-right the-list the-lists lists)
  115. nil)
  116. (apply combine
  117. (car list)
  118. (append (map car lists)
  119. (cons (recur (cdr list) (map cdr lists))
  120. '())))))))
  121. (define (fold-right1 combine nil list)
  122. (let recur ((list list))
  123. (if (null? list)
  124. nil
  125. (combine (car list) (recur (cdr list))))))
  126. (define (remp proc list)
  127. (assert-procedure 'remp proc)
  128. (let recur ((list list) (res '()))
  129. (cond ((null? list) (reverse res))
  130. ((proc (car list))
  131. (append-reverse! res (recur (cdr list) '())))
  132. (else
  133. (recur (cdr list) (cons (car list) res))))))
  134. ;; Poor man's inliner
  135. (define-syntax define-remove-like
  136. (syntax-rules ()
  137. ((define-remove-like ?name ?equal?)
  138. (define (?name obj list)
  139. (let recur ((list list) (res '()))
  140. (cond ((null? list) (reverse res))
  141. ((?equal? obj (car list))
  142. (append-reverse! res (recur (cdr list) '())))
  143. (else
  144. (recur (cdr list) (cons (car list) res)))))))))
  145. (define-remove-like remove equal?)
  146. (define-remove-like remv eqv?)
  147. (define-remove-like remq eq?)
  148. (define (append-reverse! l1 l2)
  149. (let loop ((list l1) (res l2))
  150. (cond ((null? list)
  151. res)
  152. (else
  153. (let ((next (cdr list)))
  154. (set-cdr! list res)
  155. (loop next list))))))
  156. (define (memp proc list)
  157. (assert-procedure 'member proc)
  158. (let loop ((list list))
  159. (cond ((null? list) #f)
  160. ((proc (car list)) list)
  161. (else (loop (cdr list))))))
  162. (define-syntax define-member-like
  163. (syntax-rules ()
  164. ((define-member-like ?name ?equal?)
  165. (define (?name obj list)
  166. (let loop ((list list))
  167. (cond ((null? list) #f)
  168. ((?equal? obj (car list)) list)
  169. (else (loop (cdr list)))))))))
  170. ; take the versions from `scheme'
  171. ;(define-member-like member equal?)
  172. ;(define-member-like memv eqv?)
  173. ;(define-member-like memq eq?)
  174. (define (assp proc alist)
  175. (assert-procedure 'assp proc)
  176. (let loop ((alist alist))
  177. (if (null? alist)
  178. #f
  179. (let ((p (car alist)))
  180. (if (proc (car p))
  181. p
  182. (loop (cdr alist)))))))
  183. (define-syntax define-assoc-like
  184. (syntax-rules ()
  185. ((define-assoc-like ?name ?equal?)
  186. (define (?name obj alist)
  187. (let loop ((alist alist))
  188. (if (null? alist)
  189. #f
  190. (let ((p (car alist)))
  191. (if (?equal? obj (car p))
  192. p
  193. (loop (cdr alist))))))))))
  194. ; take the versions from `scheme'
  195. ;(define-member-like assoc equal?)
  196. ;(define-member-like assv eqv?)
  197. ;(define-member-like assq eq?)
  198. (define (cons* obj . objs)
  199. (if (null? objs)
  200. obj
  201. (cons obj (apply cons* objs))))