/runtime/comlist.scm

http://github.com/digego/extempore · Scheme · 316 lines · 238 code · 14 blank · 64 comment · 0 complexity · 2cf3f4a5d11a372037f5103b8e89d484 MD5 · raw file

  1. ;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
  2. ; Copyright (C) 1991, 1993, 1995, 2001, 2003 Aubrey Jaffer.
  3. ; Copyright (C) 2000 Colin Walters
  4. ;
  5. ;Permission to copy this software, to modify it, to redistribute it,
  6. ;to distribute modified versions, and to use it for any purpose is
  7. ;granted, subject to the following restrictions and understandings.
  8. ;
  9. ;1. Any copy made of this software must include this copyright notice
  10. ;in full.
  11. ;
  12. ;2. I have made no warranty or representation that the operation of
  13. ;this software will be error-free, and I am under no obligation to
  14. ;provide any services, by way of maintenance, update, or otherwise.
  15. ;
  16. ;3. In conjunction with products arising from the use of this
  17. ;material, there shall be no use of my name in any advertising,
  18. ;promotional, or sales literature without prior written consent in
  19. ;each case.
  20. ;;; Some of these functions may be already defined in your Scheme.
  21. ;;; Comment out those definitions for functions which are already defined.
  22. ;;;; LIST FUNCTIONS FROM COMMON LISP
  23. (define (cl:assoc-adjoin pair lst)
  24. (if (assoc (car pair) lst)
  25. lst
  26. (cons pair lst)))
  27. ;; with precedence to first lst
  28. (define cl:assoc-union
  29. (letrec ((onion (lambda (lst1 lst2)
  30. (if (null? lst1)
  31. lst2
  32. (onion (cdr lst1) (cl:assoc-adjoin (car lst1) lst2))))))
  33. (lambda (lst1 lst2)
  34. (cond ((null? lst1) lst2)
  35. ((null? lst2) lst1)
  36. (else (onion (reverse lst2) lst1))))))
  37. ;;; Some tail-recursive optimizations made by
  38. ;;; Colin Walters <walters@cis.ohio-state.edu>
  39. ;;; AGJ restored order July 2001.
  40. ;;;@ From: hugh@ear.mit.edu (Hugh Secker-Walker)
  41. (define (cl:make-list k . init)
  42. (set! init (if (pair? init) (car init)))
  43. (do ((k (+ -1 k) (+ -1 k))
  44. (result '() (cons init result)))
  45. ((negative? k) result)))
  46. ;@
  47. (define (cl:copy-list lst) (append lst '()))
  48. ;@
  49. (define (cl:adjoin obj lst) (if (member obj lst) lst (cons obj lst)))
  50. ;@
  51. (define cl:union
  52. (letrec ((onion
  53. (lambda (lst1 lst2)
  54. (if (null? lst1)
  55. lst2
  56. (onion (cdr lst1) (cl:adjoin (car lst1) lst2))))))
  57. (lambda (lst1 lst2)
  58. (cond ((null? lst1) lst2)
  59. ((null? lst2) lst1)
  60. ((null? (cdr lst1)) (cl:adjoin (car lst1) lst2))
  61. ((null? (cdr lst2)) (cl:adjoin (car lst2) lst1))
  62. ((< (length lst2) (length lst1)) (onion (reverse lst2) lst1))
  63. (else (onion (reverse lst1) lst2))))))
  64. ;@
  65. (define (cl:intersection lst1 lst2)
  66. (if (null? lst2)
  67. lst2
  68. (let build-intersection ((lst1 lst1)
  69. (result '()))
  70. (cond ((null? lst1)
  71. (if (null? result)
  72. '()
  73. (reverse result)))
  74. ((member (car lst1) lst2)
  75. (build-intersection (cdr lst1) (cons (car lst1) result)))
  76. (else (build-intersection (cdr lst1) result))))))
  77. ;@
  78. (define (cl:set-difference lst1 lst2)
  79. (if (null? lst2)
  80. lst1
  81. (let build-difference ((lst1 lst1)
  82. (result '()))
  83. (cond ((null? lst1) (reverse result))
  84. ((member (car lst1) lst2) (build-difference (cdr lst1) result))
  85. (else (build-difference (cdr lst1) (cons (car lst1) result)))))))
  86. ;@
  87. (define (cl:subset? lst1 lst2)
  88. (or (eq? lst1 lst2)
  89. (let loop ((lst1 lst1))
  90. (or (null? lst1)
  91. (and (member (car lst1) lst2)
  92. (loop (cdr lst1)))))))
  93. ;@
  94. (define (cl:position obj lst)
  95. (define pos (lambda (n lst)
  96. (cond ((null? lst) #f)
  97. ((equal? obj (car lst)) n)
  98. (else (pos (+ 1 n) (cdr lst))))))
  99. (pos 0 lst))
  100. ;@
  101. (define (cl:reduce-init pred? init lst)
  102. (if (null? lst)
  103. init
  104. (cl:reduce-init pred? (pred? init (car lst)) (cdr lst))))
  105. ;@
  106. (define (cl:reduce pred? lst)
  107. (cond ((null? lst) lst)
  108. ((null? (cdr lst)) (car lst))
  109. (else (cl:reduce-init pred? (car lst) (cdr lst)))))
  110. ;@
  111. (define (cl:some pred lst . rest)
  112. (cond ((null? rest)
  113. (let mapf ((lst lst))
  114. (and (not (null? lst))
  115. (or (pred (car lst)) (mapf (cdr lst))))))
  116. (else (let mapf ((lst lst) (rest rest))
  117. (and (not (null? lst))
  118. (or (apply pred (car lst) (map car rest))
  119. (mapf (cdr lst) (map cdr rest))))))))
  120. ;@
  121. (define (cl:every pred lst . rest)
  122. (cond ((null? rest)
  123. (let mapf ((lst lst))
  124. (or (null? lst)
  125. (and (pred (car lst)) (mapf (cdr lst))))))
  126. (else (let mapf ((lst lst) (rest rest))
  127. (or (null? lst)
  128. (and (apply pred (car lst) (map car rest))
  129. (mapf (cdr lst) (map cdr rest))))))))
  130. ;@
  131. (define (cl:notany pred . ls) (not (apply cl:some pred ls)))
  132. ;@
  133. (define (cl:notevery pred . ls) (not (apply cl:every pred ls)))
  134. ;@
  135. (define (cl:list-of?? predicate . bound)
  136. (define (errout) (apply slib:error 'list-of?? predicate bound))
  137. (case (length bound)
  138. ((0)
  139. (lambda (obj)
  140. (and (list? obj)
  141. (cl:every predicate obj))))
  142. ((1)
  143. (set! bound (car bound))
  144. (cond ((negative? bound)
  145. (set! bound (- bound))
  146. (lambda (obj)
  147. (and (list? obj)
  148. (<= bound (length obj))
  149. (cl:every predicate obj))))
  150. (else
  151. (lambda (obj)
  152. (and (list? obj)
  153. (<= (length obj) bound)
  154. (cl:every predicate obj))))))
  155. ((2)
  156. (let ((low (car bound))
  157. (high (cadr bound)))
  158. (cond ((or (negative? low) (negative? high)) (errout))
  159. ((< high low)
  160. (set! high (car bound))
  161. (set! low (cadr bound))))
  162. (lambda (obj)
  163. (and (list? obj)
  164. (<= low (length obj) high)
  165. (cl:every predicate obj)))))
  166. (else (errout))))
  167. ;@
  168. (define (cl:find-if pred? lst)
  169. (cond ((null? lst) #f)
  170. ((pred? (car lst)) (car lst))
  171. (else (cl:find-if pred? (cdr lst)))))
  172. ;@
  173. (define (cl:member-if pred? lst)
  174. (cond ((null? lst) #f)
  175. ((pred? (car lst)) lst)
  176. (else (cl:member-if pred? (cdr lst)))))
  177. ;@
  178. (define (cl:remove obj lst)
  179. (define head (list '*head*))
  180. (let remove ((lst lst)
  181. (tail head))
  182. (cond ((null? lst))
  183. ((eqv? obj (car lst)) (remove (cdr lst) tail))
  184. (else
  185. (set-cdr! tail (list (car lst)))
  186. (remove (cdr lst) (cdr tail)))))
  187. (cdr head))
  188. ;@
  189. (define (cl:remove-if pred? lst)
  190. (let remove-if ((lst lst)
  191. (result '()))
  192. (cond ((null? lst) (reverse result))
  193. ((pred? (car lst)) (remove-if (cdr lst) result))
  194. (else (remove-if (cdr lst) (cons (car lst) result))))))
  195. ;@
  196. (define (cl:remove-if-not pred? lst)
  197. (let remove-if-not ((lst lst)
  198. (result '()))
  199. (cond ((null? lst) (reverse result))
  200. ((pred? (car lst)) (remove-if-not (cdr lst) (cons (car lst) result)))
  201. (else (remove-if-not (cdr lst) result)))))
  202. ;@
  203. (define cl:nconc
  204. (lambda args
  205. (cond ((null? args) '())
  206. ((null? (cdr args)) (car args))
  207. ((null? (car args)) (apply cl:nconc (cdr args)))
  208. (else
  209. (set-cdr! (last-pair (car args))
  210. (apply cl:nconc (cdr args)))
  211. (car args)))))
  212. ;;;@ From: hugh@ear.mit.edu (Hugh Secker-Walker)
  213. (define (cl:nreverse rev-it)
  214. ;;; Reverse order of elements of LIST by mutating cdrs.
  215. (cond ((null? rev-it) rev-it)
  216. ((not (list? rev-it))
  217. (slib:error "nreverse: Not a list in arg1" rev-it))
  218. (else (do ((reved '() rev-it)
  219. (rev-cdr (cdr rev-it) (cdr rev-cdr))
  220. (rev-it rev-it rev-cdr))
  221. ((begin (set-cdr! rev-it reved) (null? rev-cdr)) rev-it)))))
  222. ;@
  223. (define (cl:last lst n)
  224. (cl:nthcdr (- (length lst) n) lst))
  225. ;@
  226. (define (cl:butlast lst n)
  227. (cl:butnthcdr (- (length lst) n) lst))
  228. ;@
  229. (define (cl:nthcdr n lst)
  230. (if (zero? n) lst (cl:nthcdr (+ -1 n) (cdr lst))))
  231. ;@
  232. (define (cl:butnthcdr k lst)
  233. (cond ((negative? k) lst) ;(slib:error "negative argument to butnthcdr" k)
  234. ; SIMSYNCH FIFO8 uses negative k.
  235. ((or (zero? k) (null? lst)) '())
  236. (else (let ((ans (list (car lst))))
  237. (do ((lst (cdr lst) (cdr lst))
  238. (tail ans (cdr tail))
  239. (k (+ -2 k) (+ -1 k)))
  240. ((or (negative? k) (null? lst)) ans)
  241. (set-cdr! tail (list (car lst))))))))
  242. ;;;; CONDITIONALS
  243. ;@
  244. (define (cl:and? . args)
  245. (cond ((null? args) #t)
  246. ((car args) (apply cl:and? (cdr args)))
  247. (else #f)))
  248. ;@
  249. (define (cl:or? . args)
  250. (cond ((null? args) #f)
  251. ((car args) #t)
  252. (else (apply cl:or? (cdr args)))))
  253. ;;;@ Checks to see if a list has any duplicate MEMBERs.
  254. (define (cl:has-duplicates? lst)
  255. (cond ((null? lst) #f)
  256. ((member (car lst) (cdr lst)) #t)
  257. (else (cl:has-duplicates? (cdr lst)))))
  258. ;;;@ remove duplicates of MEMBERs of a list
  259. (define cl:remove-duplicates
  260. (letrec ((rem-dup (lambda (lst nlst)
  261. (cond ((null? lst) (if (null? nlst) nlst (reverse nlst)))
  262. ((member (car lst) nlst) (rem-dup (cdr lst) nlst))
  263. (else (rem-dup (cdr lst) (cons (car lst) nlst)))))))
  264. (lambda (lst)
  265. (rem-dup lst '()))))
  266. ;@
  267. (define cl:list*
  268. (letrec ((list*1 (lambda (obj)
  269. (if (null? (cdr obj))
  270. (car obj)
  271. (cons (car obj) (list*1 (cdr obj)))))))
  272. (lambda (obj1 . obj2)
  273. (if (null? obj2)
  274. obj1
  275. (cons obj1 (list*1 obj2))))))
  276. ;@
  277. (define (cl:atom? obj)
  278. (not (pair? obj)))
  279. ;@
  280. (define (cl:delete obj lst)
  281. (let delete ((lst lst))
  282. (cond ((null? lst) '())
  283. ((equal? obj (car lst)) (delete (cdr lst)))
  284. (else
  285. (set-cdr! lst (delete (cdr lst)))
  286. lst))))
  287. ;@
  288. (define (cl:delete-if pred lst)
  289. (let delete-if ((lst lst))
  290. (cond ((null? lst) '())
  291. ((pred (car lst)) (delete-if (cdr lst)))
  292. (else
  293. (set-cdr! lst (delete-if (cdr lst)))
  294. lst))))
  295. ;@
  296. (define (cl:delete-if-not pred lst)
  297. (let delete-if ((lst lst))
  298. (cond ((null? lst) '())
  299. ((not (pred (car lst))) (delete-if (cdr lst)))
  300. (else
  301. (set-cdr! lst (delete-if (cdr lst)))
  302. lst))))