Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define binary-search
- (lambda (n l low high)
- (if(> low high)
- -1
- (let((mid (floor (/(+ low high) 2))))
- (cond ((= (list-ref l mid) n) mid)
- ((> n (list-ref l mid)) (binary-search n l (+ mid 1) high))
- (else (binary-search n l low (- mid 1)))
- )
- )
- )
- )
- )
- (define belong?
- (lambda (n l)
- (>= (binary-search n l 0 (- (length l) 1)) 0)
- )
- )
- (define position
- (lambda (n l)
- (binary-search n l 0 (- (length l) 1))
- )
- )
- (define insert-rec
- (lambda (l n pos)
- (cond((null? l) (list n))
- ((or (= pos 0) (= pos -1)) (cons n l))
- (else (cons (car l) (insert-rec (cdr l) n (- pos 1))))
- )
- )
- )
- (define sorted-ins-rec
- (lambda(n l low high)
- (if(null? l)
- (append (list n) l)
- (let((mid (floor(/ (+ high low) 2))))
- (cond ((belong? n l) l)
- ((> n (list-ref l high)) (insert-rec l n (+ high 1)))
- ((< n (list-ref l low)) (insert-rec l n low))
- ((> n (list-ref l mid)) (sorted-ins-rec n l (+ mid 1) high))
- (else (sorted-ins-rec n l low (- mid 1)))
- )
- )
- )
- )
- )
- (define sorted-ins
- (lambda (n l)
- (sorted-ins-rec n l 0 (- (length l) 1))
- )
- )
- (define sorted-list-rec
- (lambda (l1 l2)
- (if(null? l1)
- l2
- (sorted-list-rec (cdr l1) (sorted-ins (car l1) l2))
- )
- )
- )
- (define sorted-list
- (lambda(l)
- (sorted-list-rec l '())
- )
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement