blob: 71d773d139aa6f7289d2ebb30d5dc9e407d0a420 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#lang racket
;; linear scan required for this one, but on average we save some time because sometimes we can quickly
;; adjoin small values and other times a full linear scan is required to add a high value.
(define (adjoin-set x myset)
(cond ((null? myset) (cons x '()))
((= (car myset) x) myset)
((> (car myset) x) (cons x myset))
(else (cons (car myset)
(adjoin-set x (cdr myset))))))
(define test-set (list 1 2 3 4 5 7))
(adjoin-set 6 test-set)
(adjoin-set 8 test-set)
|