Debian Codegraph @

%3a132/sortp.scm

raw symbols SCIP
1 ;;; The sort package -- sorted predicates
2 ;;; Olin Shivers 10/98.
3 ;;;
4 ;;; (list-sorted? < lis) -> boolean
5 ;;; (vector-sorted? < v [start end]) -> boolean
6
7 (define (list-sorted? < list)
8 (or (not (pair? list))
9 (let lp ((prev (car list)) (tail (cdr list)))
10 (or (not (pair? tail))
11 (let ((next (car tail)))
12 (and (not (< next prev))
13 (lp next (cdr tail))))))))
14
15 (define (vector-sorted? elt< v . maybe-start+end)
16 (call-with-values
17 (lambda () (vector-start+end v maybe-start+end))
18 (lambda (start end)
19 (or (>= start end) ; Empty range
20 (let lp ((i (+ start 1)) (vi-1 (vector-ref v start)))
21 (or (>= i end)
22 (let ((vi (vector-ref v i)))
23 (and (not (elt< vi vi-1))
24 (lp (+ i 1) vi)))))))))
25
26 ;;; Copyright and porting non-notices
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Give me a break. It's fifteen lines of code. I place this code in the
29 ;;; public domain; help yourself.
30 ;;;
31 ;;; If your Scheme has a faster mechanism for handling optional arguments
32 ;;; (e.g., Chez), you should definitely port over to it. Note that argument
33 ;;; defaulting and error-checking are interleaved -- you don't have to
34 ;;; error-check defaulted START/END args to see if they are fixnums that are
35 ;;; legal vector indices for the corresponding vector, etc.