common-list.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. ;;;; common-list.scm --- COMMON LISP list functions for Scheme
  2. ;;;;
  3. ;;;; Copyright (C) 1995, 1996, 1997, 2001, 2006 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 2.1 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;;
  19. ;;; Commentary:
  20. ;; These procedures are exported:
  21. ;; (adjoin e l)
  22. ;; (union l1 l2)
  23. ;; (intersection l1 l2)
  24. ;; (set-difference l1 l2)
  25. ;; (reduce-init p init l)
  26. ;; (reduce p l)
  27. ;; (some pred l . rest)
  28. ;; (every pred l . rest)
  29. ;; (notany pred . ls)
  30. ;; (notevery pred . ls)
  31. ;; (count-if pred l)
  32. ;; (find-if pred l)
  33. ;; (member-if pred l)
  34. ;; (remove-if pred l)
  35. ;; (remove-if-not pred l)
  36. ;; (delete-if! pred l)
  37. ;; (delete-if-not! pred l)
  38. ;; (butlast lst n)
  39. ;; (and? . args)
  40. ;; (or? . args)
  41. ;; (has-duplicates? lst)
  42. ;; (pick p l)
  43. ;; (pick-mappings p l)
  44. ;; (uniq l)
  45. ;;
  46. ;; See docstrings for each procedure for more info. See also module
  47. ;; `(srfi srfi-1)' for a complete list handling library.
  48. ;;; Code:
  49. (define-module (ice-9 common-list)
  50. :export (adjoin union intersection set-difference reduce-init reduce
  51. some every notany notevery count-if find-if member-if remove-if
  52. remove-if-not delete-if! delete-if-not! butlast and? or?
  53. has-duplicates? pick pick-mappings uniq))
  54. ;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
  55. ; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer.
  56. ;
  57. ;Permission to copy this software, to redistribute it, and to use it
  58. ;for any purpose is granted, subject to the following restrictions and
  59. ;understandings.
  60. ;
  61. ;1. Any copy made of this software must include this copyright notice
  62. ;in full.
  63. ;
  64. ;2. I have made no warrantee or representation that the operation of
  65. ;this software will be error-free, and I am under no obligation to
  66. ;provide any services, by way of maintenance, update, or otherwise.
  67. ;
  68. ;3. In conjunction with products arising from the use of this
  69. ;material, there shall be no use of my name in any advertising,
  70. ;promotional, or sales literature without prior written consent in
  71. ;each case.
  72. (define (adjoin e l)
  73. "Return list L, possibly with element E added if it is not already in L."
  74. (if (memq e l) l (cons e l)))
  75. (define (union l1 l2)
  76. "Return a new list that is the union of L1 and L2.
  77. Elements that occur in both lists occur only once in
  78. the result list."
  79. (cond ((null? l1) l2)
  80. ((null? l2) l1)
  81. (else (union (cdr l1) (adjoin (car l1) l2)))))
  82. (define (intersection l1 l2)
  83. "Return a new list that is the intersection of L1 and L2.
  84. Only elements that occur in both lists occur in the result list."
  85. (if (null? l2) l2
  86. (let loop ((l1 l1) (result '()))
  87. (cond ((null? l1) (reverse! result))
  88. ((memv (car l1) l2) (loop (cdr l1) (cons (car l1) result)))
  89. (else (loop (cdr l1) result))))))
  90. (define (set-difference l1 l2)
  91. "Return elements from list L1 that are not in list L2."
  92. (let loop ((l1 l1) (result '()))
  93. (cond ((null? l1) (reverse! result))
  94. ((memv (car l1) l2) (loop (cdr l1) result))
  95. (else (loop (cdr l1) (cons (car l1) result))))))
  96. (define (reduce-init p init l)
  97. "Same as `reduce' except it implicitly inserts INIT at the start of L."
  98. (if (null? l)
  99. init
  100. (reduce-init p (p init (car l)) (cdr l))))
  101. (define (reduce p l)
  102. "Combine all the elements of sequence L using a binary operation P.
  103. The combination is left-associative. For example, using +, one can
  104. add up all the elements. `reduce' allows you to apply a function which
  105. accepts only two arguments to more than 2 objects. Functional
  106. programmers usually refer to this as foldl."
  107. (cond ((null? l) l)
  108. ((null? (cdr l)) (car l))
  109. (else (reduce-init p (car l) (cdr l)))))
  110. (define (some pred l . rest)
  111. "PRED is a boolean function of as many arguments as there are list
  112. arguments to `some', i.e., L plus any optional arguments. PRED is
  113. applied to successive elements of the list arguments in order. As soon
  114. as one of these applications returns a true value, return that value.
  115. If no application returns a true value, return #f.
  116. All the lists should have the same length."
  117. (cond ((null? rest)
  118. (let mapf ((l l))
  119. (and (not (null? l))
  120. (or (pred (car l)) (mapf (cdr l))))))
  121. (else (let mapf ((l l) (rest rest))
  122. (and (not (null? l))
  123. (or (apply pred (car l) (map car rest))
  124. (mapf (cdr l) (map cdr rest))))))))
  125. (define (every pred l . rest)
  126. "Return #t iff every application of PRED to L, etc., returns #t.
  127. Analogous to `some' except it returns #t if every application of
  128. PRED is #t and #f otherwise."
  129. (cond ((null? rest)
  130. (let mapf ((l l))
  131. (or (null? l)
  132. (and (pred (car l)) (mapf (cdr l))))))
  133. (else (let mapf ((l l) (rest rest))
  134. (or (null? l)
  135. (and (apply pred (car l) (map car rest))
  136. (mapf (cdr l) (map cdr rest))))))))
  137. (define (notany pred . ls)
  138. "Return #t iff every application of PRED to L, etc., returns #f.
  139. Analogous to some but returns #t if no application of PRED returns a
  140. true value or #f as soon as any one does."
  141. (not (apply some pred ls)))
  142. (define (notevery pred . ls)
  143. "Return #t iff there is an application of PRED to L, etc., that returns #f.
  144. Analogous to some but returns #t as soon as an application of PRED returns #f,
  145. or #f otherwise."
  146. (not (apply every pred ls)))
  147. (define (count-if pred l)
  148. "Return the number of elements in L for which (PRED element) returns true."
  149. (let loop ((n 0) (l l))
  150. (cond ((null? l) n)
  151. ((pred (car l)) (loop (+ n 1) (cdr l)))
  152. (else (loop n (cdr l))))))
  153. (define (find-if pred l)
  154. "Search for the first element in L for which (PRED element) returns true.
  155. If found, return that element, otherwise return #f."
  156. (cond ((null? l) #f)
  157. ((pred (car l)) (car l))
  158. (else (find-if pred (cdr l)))))
  159. (define (member-if pred l)
  160. "Return the first sublist of L for whose car PRED is true."
  161. (cond ((null? l) #f)
  162. ((pred (car l)) l)
  163. (else (member-if pred (cdr l)))))
  164. (define (remove-if pred l)
  165. "Remove all elements from L where (PRED element) is true.
  166. Return everything that's left."
  167. (let loop ((l l) (result '()))
  168. (cond ((null? l) (reverse! result))
  169. ((pred (car l)) (loop (cdr l) result))
  170. (else (loop (cdr l) (cons (car l) result))))))
  171. (define (remove-if-not pred l)
  172. "Remove all elements from L where (PRED element) is #f.
  173. Return everything that's left."
  174. (let loop ((l l) (result '()))
  175. (cond ((null? l) (reverse! result))
  176. ((not (pred (car l))) (loop (cdr l) result))
  177. (else (loop (cdr l) (cons (car l) result))))))
  178. (define (delete-if! pred l)
  179. "Destructive version of `remove-if'."
  180. (let delete-if ((l l))
  181. (cond ((null? l) '())
  182. ((pred (car l)) (delete-if (cdr l)))
  183. (else
  184. (set-cdr! l (delete-if (cdr l)))
  185. l))))
  186. (define (delete-if-not! pred l)
  187. "Destructive version of `remove-if-not'."
  188. (let delete-if-not ((l l))
  189. (cond ((null? l) '())
  190. ((not (pred (car l))) (delete-if-not (cdr l)))
  191. (else
  192. (set-cdr! l (delete-if-not (cdr l)))
  193. l))))
  194. (define (butlast lst n)
  195. "Return all but the last N elements of LST."
  196. (letrec ((l (- (length lst) n))
  197. (bl (lambda (lst n)
  198. (cond ((null? lst) lst)
  199. ((positive? n)
  200. (cons (car lst) (bl (cdr lst) (+ -1 n))))
  201. (else '())))))
  202. (bl lst (if (negative? n)
  203. (error "negative argument to butlast" n)
  204. l))))
  205. (define (and? . args)
  206. "Return #t iff all of ARGS are true."
  207. (cond ((null? args) #t)
  208. ((car args) (apply and? (cdr args)))
  209. (else #f)))
  210. (define (or? . args)
  211. "Return #t iff any of ARGS is true."
  212. (cond ((null? args) #f)
  213. ((car args) #t)
  214. (else (apply or? (cdr args)))))
  215. (define (has-duplicates? lst)
  216. "Return #t iff 2 members of LST are equal?, else #f."
  217. (cond ((null? lst) #f)
  218. ((member (car lst) (cdr lst)) #t)
  219. (else (has-duplicates? (cdr lst)))))
  220. (define (pick p l)
  221. "Apply P to each element of L, returning a list of elts
  222. for which P returns a non-#f value."
  223. (let loop ((s '())
  224. (l l))
  225. (cond
  226. ((null? l) s)
  227. ((p (car l)) (loop (cons (car l) s) (cdr l)))
  228. (else (loop s (cdr l))))))
  229. (define (pick-mappings p l)
  230. "Apply P to each element of L, returning a list of the
  231. non-#f return values of P."
  232. (let loop ((s '())
  233. (l l))
  234. (cond
  235. ((null? l) s)
  236. ((p (car l)) => (lambda (mapping) (loop (cons mapping s) (cdr l))))
  237. (else (loop s (cdr l))))))
  238. (define (uniq l)
  239. "Return a list containing elements of L, with duplicates removed."
  240. (let loop ((acc '())
  241. (l l))
  242. (if (null? l)
  243. (reverse! acc)
  244. (loop (if (memq (car l) acc)
  245. acc
  246. (cons (car l) acc))
  247. (cdr l)))))
  248. ;;; common-list.scm ends here