Use of nreverse

nreverse is useful when the natural way of writing a function causes the result to be in backwards order.

In this case, using nreverse is fast, safe, and avoids doing extra cons operations that would be done by reverse.

(define (varsin expr)  ; variables in expression
  (nreverse (varsinb expr '())) )

(define (varsinb expr answer) (if (pair? expr) (varsinl (cdr expr) answer) (if (symbol? expr) (if (member expr answer) answer (cons expr answer)) answer)))

(define (varsinl lst answer) (if (pair? lst) (varsinl (cdr lst) (varsinb (car lst) answer)) answer))

Contents    Page-10    Prev    Next    Page+10    Index