These procedures may mutate the list they operate on, but any such mutation is undefined.
nconcdestructively concatenates its arguments. (Compare this withappend, which copies arguments rather than destroying them.) Sometimes calledappend!(see Rev2 Procedures).Example: You want to find the subsets of a set. Here's the obvious way:
(define (subsets set) (if (null? set) '(()) (append (map (lambda (sub) (cons (car set) sub)) (subsets (cdr set))) (subsets (cdr set)))))But that does way more consing than you need. Instead, you could replace the
appendwithnconc, since you don't have any need for all the intermediate results.Example:
(define x '(a b c)) (define y '(d e f)) (nconc x y) ⇒ (a b c d e f) x ⇒ (a b c d e f)
nconcis the same asappend!in sc2.scm.
nreversereverses the order of elements in lst by mutatingcdrs of the list. Sometimes calledreverse!.Example:
(define foo '(a b c)) (nreverse foo) ⇒ (c b a) foo ⇒ (a)Some people have been confused about how to use
nreverse, thinking that it doesn't return a value. It needs to be pointed out that(set! lst (nreverse lst))is the proper usage, not
(nreverse lst)The example should suffice to show why this is the case.
Destructive versions of
removeremove-if, andremove-if-not.Example:
(define lst (list 'foo 'bar 'baz 'bang)) (delete 'foo lst) ⇒ (bar baz bang) lst ⇒ (foo bar baz bang) (define lst (list 1 2 3 4 5 6 7 8 9)) (delete-if odd? lst) ⇒ (2 4 6 8) lst ⇒ (1 2 4 6 8)Some people have been confused about how to use
delete,delete-if, anddelete-if, thinking that they don't return a value. It needs to be pointed out that(set! lst (delete el lst))is the proper usage, not
(delete el lst)The examples should suffice to show why this is the case.