Consider this function which adjoins one element 'x' to a set 'S', non-destructively.
Code: Select all
(define (adjoin1 S x)
(if (or (= x nil) (= S nil) (member x S))
S
(cons x S)))
> (define L '(1 2 3))
(1 2 3)
> (adjoin1 L 0)
(0 1 2 3)
Code: Select all
(define (adjoin S)
(if (empty? (args))
S
(apply adjoin
(cons (adjoin1 S (first (args)))
(rest (args))))))
Now, yes, I know that the alternative definition
Code: Select all
(define (adjoin S)
(if (empty? (args))
S
(let ((result S))
(dolist (x (args))
(set! result (adjoin1 result x))))))
Code: Select all
(define (adjoin S) (unique (append S (args))))
Thanks! --Ricky