File: list.scm

package info (click to toggle)
scheme48 1.9-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 18,276 kB
  • ctags: 16,390
  • sloc: lisp: 88,906; ansic: 87,511; sh: 3,224; makefile: 766
file content (228 lines) | stat: -rw-r--r-- 5,899 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
; Part of Scheme 48 1.9.  See file COPYING for notices and license.

; Authors: Mike Sperber

; The (rnrs lists (6)) library.

(define (assert-procedure who obj)
  (if (not (procedure? obj))
      (assertion-violation who "not a procedure" obj)))

(define (find proc list)
  (assert-procedure 'find proc)
  (let loop ((list list))
    (cond
     ((null? list) #f)
     ((proc (car list)) (car list))
     (else (loop (cdr list))))))

(define (check-nulls who the-list the-lists lists)
  (for-each (lambda (list)
	      (if (not (null? list))
		  (apply assertion-violation who
			 "argument lists don't have the same size"
			 list lists)))
	    lists))

(define (for-all proc list . lists)
  (assert-procedure 'for-all proc)
  (cond
   ((null? lists)
    (for-all1 proc list))
   ((null? list)
    (check-nulls 'for-all list lists lists)
    #t)
   (else
    (let loop ((list list) (lists lists))
      (let ((next (cdr list)))
	(cond
	 ((null? next)
	  (apply proc (car list) (map car lists))) 
	 ((apply proc (car list) (map car lists))
	  (loop next (map cdr lists)))
	 (else #f)))))))

(define (for-all1 proc list)
  (if (null? list)
      #t
      (let loop ((list list))
	(let ((next (cdr list)))
	  (cond
	   ((null? next) (proc (car list))) 
	   ((proc (car list)) (loop next))
	   (else #f))))))

(define (exists proc list . lists)
  (assert-procedure 'exists proc)
  (cond
   ((null? lists)
    (exists1 proc list))
   ((null? list)
    (check-nulls 'exists list lists lists)
    #f)
   (else
    (let loop ((list list) (lists lists))
      (let ((next (cdr list)))
	(if (null? next)
	    (apply proc (car list) (map car lists))
	    (or (apply proc (car list) (map car lists))
		(loop next (map cdr lists)))))))))

(define (exists1 proc list)
  (if (null? list)
      #f
      (let loop ((list list))
	(let ((next (cdr list)))
	  (if (null? next)
	      (proc (car list))
	      (or (proc (car list))
		  (loop next)))))))

(define (filter proc list)
  (assert-procedure 'filter proc)
  (let loop ((list list) (r '()))
    (cond ((null? list)
	   (reverse r))
          ((proc (car list))
	   (loop (cdr list) (cons (car list) r)))
          (else
	   (loop (cdr list) r)))))

(define (partition proc list)
  (assert-procedure 'partition proc)
  (let loop ((list list) (yes '()) (no '()))
    (cond ((null? list)
           (values (reverse yes) (reverse no)))
          ((proc (car list))
           (loop (cdr list) (cons (car list) yes) no))
          (else
           (loop (cdr list) yes (cons (car list) no))))))

(define (fold-left combine nil the-list . the-lists)
  (assert-procedure 'fold-left combine)
  (if (null? the-lists)
      (fold-left1 combine nil the-list)
      (let loop ((accum nil) (list the-list) (lists the-lists))
	(if (null? list)
	    (begin
	      (check-nulls 'fold-left the-list the-lists lists)
	      accum)
	    (loop (apply combine accum (car list) (map car lists))
		  (cdr list)
		  (map cdr lists))))))

(define (fold-left1 combine nil list)
  (let loop ((accum nil) (list list))
    (if (null? list)
	accum
	(loop (combine accum (car list))
	      (cdr list)))))


(define (fold-right combine nil the-list . the-lists)
  (assert-procedure 'fold-right combine)
  (if (null? the-lists)
      (fold-right1 combine nil the-list)
      (let recur ((list the-list) (lists the-lists))
	(if (null? list)
	    (begin
	      (check-nulls 'fold-right the-list the-lists lists)
	      nil)
	    (apply combine
		   (car list)
		   (append (map car lists)
			   (cons (recur (cdr list) (map cdr lists))
				 '())))))))

(define (fold-right1 combine nil list)
  (let recur ((list list))
    (if (null? list)
	nil
	(combine (car list) (recur (cdr list))))))

(define (remp proc list)
  (assert-procedure 'remp proc)
  (let recur ((list list) (res '()))
    (cond ((null? list) (reverse res))
	  ((proc (car list))
	   (append-reverse! res (recur (cdr list) '())))
	  (else
	   (recur (cdr list) (cons (car list) res))))))

;; Poor man's inliner
(define-syntax define-remove-like
  (syntax-rules ()
    ((define-remove-like ?name ?equal?)
     (define (?name obj list)
       (let recur ((list list) (res '()))
	 (cond ((null? list) (reverse res))
	       ((?equal? obj (car list))
		(append-reverse! res (recur (cdr list) '())))
	       (else
		(recur (cdr list) (cons (car list) res)))))))))

(define-remove-like remove equal?)
(define-remove-like remv eqv?)
(define-remove-like remq eq?)

(define (append-reverse! l1 l2)
  (let loop ((list l1) (res l2))
    (cond ((null? list)
	   res)
	  (else
	   (let ((next (cdr list)))
	     (set-cdr! list res)
	     (loop next list))))))

(define (memp proc list)
  (assert-procedure 'member proc)
  (let loop ((list list))
    (cond ((null? list) #f)
	  ((proc (car list)) list)
	  (else (loop (cdr list))))))

(define-syntax define-member-like
  (syntax-rules ()
    ((define-member-like ?name ?equal?)
     (define (?name obj list)
       (let loop ((list list))
	 (cond ((null? list) #f)
	       ((?equal? obj (car list)) list)
	       (else (loop (cdr list)))))))))

; take the versions from `scheme'
;(define-member-like member equal?)
;(define-member-like memv eqv?)
;(define-member-like memq eq?)

(define (assp proc alist)
  (assert-procedure 'assp proc)
  (let loop ((alist alist))
    (if (null? alist)
	#f
	(let ((p (car alist)))
	  (if (proc (car p))
	      p
	      (loop (cdr alist)))))))

(define-syntax define-assoc-like
  (syntax-rules ()
    ((define-assoc-like ?name ?equal?)
     (define (?name obj alist)
       (let loop ((alist alist))
	 (if (null? alist)
	     #f
	     (let ((p (car alist)))
	       (if (?equal? obj (car p))
		   p
		   (loop (cdr alist))))))))))

; take the versions from `scheme'
;(define-member-like assoc equal?)
;(define-member-like assv eqv?)
;(define-member-like assq eq?)

(define (cons* obj . objs)
  (if (null? objs)
      obj
      (cons obj (apply cons* objs))))