|
89 | 89 | (let loop ([l l0] |
90 | 90 | [pos npos]) |
91 | 91 | (if (pair? l) |
92 | | - (if (eq? pos 1) (car l) (loop (cdr l) (sub1 pos))) |
| 92 | + (if (eq? pos 1) |
| 93 | + (car l) |
| 94 | + (loop (cdr l) (sub1 pos))) |
93 | 95 | (raise-arguments-error 'name "list contains too few elements" "list" l0))) |
94 | 96 | (raise-argument-error 'name "list?" l0)))])) |
95 | 97 | (define-lgetter second 2) |
|
106 | 108 | (if (pair? l) |
107 | 109 | (let loop ([l l] |
108 | 110 | [x (cdr l)]) |
109 | | - (if (pair? x) (loop x (cdr x)) l)) |
| 111 | + (if (pair? x) |
| 112 | + (loop x (cdr x)) |
| 113 | + l)) |
110 | 114 | (raise-argument-error 'last-pair "pair?" l))) |
111 | 115 |
|
112 | 116 | (define (last l) |
113 | 117 | (if (and (pair? l) (list? l)) |
114 | 118 | (let loop ([l l] |
115 | 119 | [x (cdr l)]) |
116 | | - (if (pair? x) (loop x (cdr x)) (car l))) |
| 120 | + (if (pair? x) |
| 121 | + (loop x (cdr x)) |
| 122 | + (car l))) |
117 | 123 | (raise-argument-error 'last "(and/c list? (not/c empty?))" l))) |
118 | 124 |
|
119 | 125 | (define (rest l) |
|
128 | 134 | (raise-argument-error 'make-list "exact-nonnegative-integer?" 0 n x)) |
129 | 135 | (let loop ([n n] |
130 | 136 | [r '()]) |
131 | | - (if (zero? n) r (loop (sub1 n) (cons x r))))) |
| 137 | + (if (zero? n) |
| 138 | + r |
| 139 | + (loop (sub1 n) (cons x r))))) |
132 | 140 |
|
133 | 141 | (define (list-update l i f) |
134 | 142 | (unless (list? l) |
|
150 | 158 |
|
151 | 159 | ;; internal use below |
152 | 160 | (define (drop* list n) ; no error checking, returns #f if index is too large |
153 | | - (if (zero? n) list (and (pair? list) (drop* (cdr list) (sub1 n))))) |
| 161 | + (if (zero? n) |
| 162 | + list |
| 163 | + (and (pair? list) (drop* (cdr list) (sub1 n))))) |
154 | 164 | (define (too-large who list n) |
155 | 165 | (define proper? (list? list)) |
156 | 166 | (raise-argument-error who |
|
192 | 202 | (raise-argument-error 'takef "procedure?" 1 list pred)) |
193 | 203 | (let loop ([list list]) |
194 | 204 | (if (pair? list) |
195 | | - (let ([x (car list)]) (if (pred x) (cons x (loop (cdr list))) '())) |
| 205 | + (let ([x (car list)]) |
| 206 | + (if (pred x) |
| 207 | + (cons x (loop (cdr list))) |
| 208 | + '())) |
196 | 209 | ;; could return `list' here, but make it behave like `take' |
197 | 210 | ;; example: (takef '(a b c . d) symbol?) should be similar |
198 | 211 | ;; to (take '(a b c . d) 3) |
|
202 | 215 | (unless (procedure? pred) |
203 | 216 | (raise-argument-error 'dropf "procedure?" 1 list pred)) |
204 | 217 | (let loop ([list list]) |
205 | | - (if (and (pair? list) (pred (car list))) (loop (cdr list)) list))) |
| 218 | + (if (and (pair? list) (pred (car list))) |
| 219 | + (loop (cdr list)) |
| 220 | + list))) |
206 | 221 |
|
207 | 222 | (define (splitf-at list pred) |
208 | 223 | (unless (procedure? pred) |
|
221 | 236 | (let loop ([list list] |
222 | 237 | [lead (or (drop* list n) (too-large 'take-right list n))]) |
223 | 238 | ;; could throw an error for non-lists, but be more like `take' |
224 | | - (if (pair? lead) (loop (cdr list) (cdr lead)) list))) |
| 239 | + (if (pair? lead) |
| 240 | + (loop (cdr list) (cdr lead)) |
| 241 | + list))) |
225 | 242 |
|
226 | 243 | (define (drop-right list n) |
227 | 244 | (unless (exact-nonnegative-integer? n) |
228 | 245 | (raise-argument-error 'drop-right "exact-nonnegative-integer?" 1 list n)) |
229 | 246 | (let loop ([list list] |
230 | 247 | [lead (or (drop* list n) (too-large 'drop-right list n))]) |
231 | 248 | ;; could throw an error for non-lists, but be more like `drop' |
232 | | - (if (pair? lead) (cons (car list) (loop (cdr list) (cdr lead))) '()))) |
| 249 | + (if (pair? lead) |
| 250 | + (cons (car list) (loop (cdr list) (cdr lead))) |
| 251 | + '()))) |
233 | 252 |
|
234 | 253 | (define (split-at-right list n) |
235 | 254 | (unless (exact-nonnegative-integer? n) |
|
238 | 257 | [lead (or (drop* list n) (too-large 'split-at-right list n))] |
239 | 258 | [pfx '()]) |
240 | 259 | ;; could throw an error for non-lists, but be more like `split-at' |
241 | | - (if (pair? lead) (loop (cdr list) (cdr lead) (cons (car list) pfx)) (values (reverse pfx) list)))) |
| 260 | + (if (pair? lead) |
| 261 | + (loop (cdr list) (cdr lead) (cons (car list) pfx)) |
| 262 | + (values (reverse pfx) list)))) |
242 | 263 |
|
243 | 264 | ;; For just `takef-right', it's possible to do something smart that |
244 | 265 | ;; scans the list in order, keeping a pointer to the beginning of the |
|
265 | 286 | (loop (cdr list) (cons (car list) rev) (add1 n)) |
266 | 287 | (let loop ([n n] |
267 | 288 | [list rev]) |
268 | | - (if (and (pair? list) (pred (car list))) (loop (sub1 n) (cdr list)) n))))) |
| 289 | + (if (and (pair? list) (pred (car list))) |
| 290 | + (loop (sub1 n) (cdr list)) |
| 291 | + n))))) |
269 | 292 |
|
270 | 293 | (define (takef-right list pred) |
271 | 294 | (drop list (count-from-right 'takef-right list pred))) |
|
371 | 394 | (check-not-given before-first "#:before-first") |
372 | 395 | (check-not-given after-last "#:after-last")]) |
373 | 396 | (cond |
374 | | - [(or (null? l) (null? (cdr l))) (if splice? (append before-first l after-last) l)] |
| 397 | + [(or (null? l) (null? (cdr l))) |
| 398 | + (if splice? |
| 399 | + (append before-first l after-last) |
| 400 | + l)] |
375 | 401 | ;; two cases for efficiency, maybe not needed |
376 | 402 | [splice? |
377 | 403 | (let* ([x (reverse x)] |
|
452 | 478 | (begin |
453 | 479 | (hash-set! h k #t) |
454 | 480 | (cons x (loop l)))))))])]) |
455 | | - (if key (loop key) (loop no-key)))]))) |
| 481 | + (if key |
| 482 | + (loop key) |
| 483 | + (loop no-key)))]))) |
456 | 484 |
|
457 | 485 | ;; check-duplicates : (listof X) |
458 | 486 | ;; [(K K -> bool)] |
|
466 | 494 | (raise-argument-error 'check-duplicates "list?" 0 items)) |
467 | 495 | (unless (and (procedure? key) (procedure-arity-includes? key 1)) |
468 | 496 | (raise-argument-error 'check-duplicates "(-> any/c any/c)" key)) |
469 | | - (let ([fail-k (if (procedure? failure-result) failure-result (λ () failure-result))]) |
| 497 | + (let ([fail-k (if (procedure? failure-result) |
| 498 | + failure-result |
| 499 | + (λ () failure-result))]) |
470 | 500 | (cond |
471 | 501 | [(eq? same? equal?) (check-duplicates/t items key (make-hash) fail-k)] |
472 | 502 | [(eq? same? eq?) (check-duplicates/t items key (make-hasheq) fail-k)] |
|
532 | 562 | (if (null? l) |
533 | 563 | null |
534 | 564 | (let ([x (apply f (car l) (map car ls))]) |
535 | | - (if x (cons x (loop (cdr l) (map cdr ls))) (loop (cdr l) (map cdr ls)))))) |
| 565 | + (if x |
| 566 | + (cons x (loop (cdr l) (map cdr ls))) |
| 567 | + (loop (cdr l) (map cdr ls)))))) |
536 | 568 | (raise-arguments-error 'filter-map "all lists must have same size"))) |
537 | 569 | (let loop ([l l]) |
538 | | - (if (null? l) null (let ([x (f (car l))]) (if x (cons x (loop (cdr l))) (loop (cdr l)))))))) |
| 570 | + (if (null? l) |
| 571 | + null |
| 572 | + (let ([x (f (car l))]) |
| 573 | + (if x |
| 574 | + (cons x (loop (cdr l))) |
| 575 | + (loop (cdr l)))))))) |
539 | 576 |
|
540 | 577 | ;; very similar to `filter-map', one more such function will justify some macro |
541 | 578 | (define (count f l . ls) |
|
548 | 585 | [c 0]) |
549 | 586 | (if (null? l) |
550 | 587 | c |
551 | | - (loop (cdr l) (map cdr ls) (if (apply f (car l) (map car ls)) (add1 c) c)))) |
| 588 | + (loop (cdr l) |
| 589 | + (map cdr ls) |
| 590 | + (if (apply f (car l) (map car ls)) |
| 591 | + (add1 c) |
| 592 | + c)))) |
552 | 593 | (raise-arguments-error 'count "all lists must have same size"))) |
553 | 594 | (let loop ([l l] |
554 | 595 | [c 0]) |
555 | | - (if (null? l) c (loop (cdr l) (if (f (car l)) (add1 c) c)))))) |
| 596 | + (if (null? l) |
| 597 | + c |
| 598 | + (loop (cdr l) |
| 599 | + (if (f (car l)) |
| 600 | + (add1 c) |
| 601 | + c)))))) |
556 | 602 |
|
557 | 603 | ;; Originally from srfi-1 -- shares common tail with the input when possible |
558 | 604 | ;; (define (partition f l) |
|
581 | 627 | (values (reverse i) (reverse o)) |
582 | 628 | (let ([x (car l)] |
583 | 629 | [l (cdr l)]) |
584 | | - (if (pred x) (loop l (cons x i) o) (loop l i (cons x o))))))) |
| 630 | + (if (pred x) |
| 631 | + (loop l (cons x i) o) |
| 632 | + (loop l i (cons x o))))))) |
585 | 633 |
|
586 | 634 | ;; similar to in-range, but returns a list |
587 | 635 | (define range-proc |
|
647 | 695 | ;; faster than a plain loop |
648 | 696 | (let loop ([l list] |
649 | 697 | [result null]) |
650 | | - (if (null? l) (reverse result) (loop (cdr l) (if (f (car l)) result (cons (car l) result)))))) |
| 698 | + (if (null? l) |
| 699 | + (reverse result) |
| 700 | + (loop (cdr l) |
| 701 | + (if (f (car l)) |
| 702 | + result |
| 703 | + (cons (car l) result)))))) |
651 | 704 |
|
652 | 705 | ;; Fisher-Yates Shuffle |
653 | 706 | (define (shuffle l) |
|
689 | 742 | (let ([curr (unbox curr-box)]) |
690 | 743 | (if (< curr limit) |
691 | 744 | (begin0 (for/fold ([acc '()]) ([i (in-range N-1 -1 -1)]) |
692 | | - (if (bitwise-bit-set? curr i) (cons (vector-ref v i) acc) acc)) |
| 745 | + (if (bitwise-bit-set? curr i) |
| 746 | + (cons (vector-ref v i) acc) |
| 747 | + acc)) |
693 | 748 | (set-box! curr-box (+ curr 1))) |
694 | 749 | #f)))] |
695 | 750 | [(< N k) (lambda () #f)] |
|
0 commit comments