[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/gBjAOpD4    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Nov 19:
; list intersection and union

(define (make-hash hash eql? oops size)
  (let ((table (make-vector size '())))
    (lambda (message . args)
      (if (eq? message 'enlist)
          (let loop ((k 0) (result '()))
            (if (= size k)
                result
                (loop (+ k 1) (append (vector-ref table k) result))))
          (let* ((key (car args))
                 (index (modulo (hash key) size))
                 (bucket (vector-ref table index)))
            (case message
              ((lookup fetch get ref recall)
                (let loop ((bucket bucket))
                  (cond ((null? bucket) oops)
                        ((eql? (caar bucket) key) (cdar bucket))
                        (else (loop (cdr bucket))))))
              ((insert insert! ins ins! set set! store store! install install!)
                (vector-set! table index
                  (let loop ((bucket bucket))
                    (cond ((null? bucket)
                            (list (cons key (cadr args))))
                          ((eql? (caar bucket) key)
                            (cons (cons key (cadr args)) (cdr bucket)))
                          (else (cons (car bucket) (loop (cdr bucket))))))))
              ((delete delete! del del! remove remove!)
                (vector-set! table index
                  (let loop ((bucket bucket))
                    (cond ((null? bucket) '())
                          ((eql? (caar bucket) key)
                            (cdr bucket))
                          (else (cons (car bucket) (loop (cdr bucket))))))))
              ((update update!)
                (vector-set! table index
                  (let loop ((bucket bucket))
                    (cond ((null? bucket)
                            (list (cons key (caddr args))))
                          ((eql? (caar bucket) key)
                            (cons (cons key ((cadr args) key (cdar bucket))) (cdr bucket)))
                          (else (cons (car bucket) (loop (cdr bucket))))))))
              (else (error 'hash-table "unrecognized message")) ))))))

(define sort #f)
(define merge #f)
(let ()
  (define dosort
    (lambda (pred? ls n)
      (if (= n 1)
          (list (car ls))
          (let ((i (quotient n 2)))
            (domerge pred?
                     (dosort pred? ls i)
                     (dosort pred? (list-tail ls i) (- n i)))))))
  (define domerge
    (lambda (pred? l1 l2)
      (cond
        ((null? l1) l2)
        ((null? l2) l1)
        ((pred? (car l2) (car l1))
         (cons (car l2) (domerge pred? l1 (cdr l2))))
        (else (cons (car l1) (domerge pred? (cdr l1) l2))))))
  (set! sort
    (lambda (pred? l)
      (if (null? l) l (dosort pred? l (length l)))))
  (set! merge
    (lambda (pred? l1 l2)
      (domerge pred? l1 l2))))

(define (identity x) x)

(define (difference1 xs ys)
  (let loop ((xs xs) (zs (list)))
    (cond ((null? xs) zs)
          ((member (car xs) ys)
            (loop (cdr xs) zs))
          (else (loop (cdr xs) (cons (car xs) zs))))))

(define (difference2 xs ys)
  (let loop ((xs (sort < xs)) (ys (sort < ys)) (zs (list)))
    (cond ((null? xs) zs)
          ((null? ys) (append xs zs))
          ((< (car xs) (car ys))
            (loop (cdr xs) ys (cons (car xs) zs)))
          ((< (car ys) (car xs))
            (loop xs (cdr ys) zs))
          (else (loop (cdr xs) (cdr ys) zs)))))

(define (difference3 xs ys)
  (let ((h (make-hash identity = #f 9973)))
    (do ((ys ys (cdr ys))) ((null? ys))
      (h 'insert (car ys) (car ys)))
    (let loop ((xs xs) (zs (list)))
      (cond ((null? xs) zs)
            ((h 'lookup (car xs))
              (loop (cdr xs) zs))
            (else (loop (cdr xs) (cons (car xs) zs)))))))

(define a (list 4 7 12 6 17 5 13))
(define b (list 7 19 4 11 13 2 15))
(display (difference1 a b)) (newline)
(display (difference1 b a)) (newline)
(display (difference2 a b)) (newline)
(display (difference2 b a)) (newline)
(display (difference3 a b)) (newline)
(display (difference3 b a)) (newline)


Output:
1
2
3
4
5
6
(5 17 6 12)
(15 2 11 19)
(17 12 6 5)
(19 15 11 2)
(5 17 6 12)
(15 2 11 19)


Create a new paste based on this one


Comments: