Next: , Previous: , Up: General Efficiency Hints   [Contents][Index]


5.12.4 Mapping and Iteration

One of the traditional Common Lisp programming styles is a highly applicative one, involving the use of mapping functions and many lists to store intermediate results. To compute the sum of the square-roots of a list of numbers, one might say:

(apply #'+ (mapcar #'sqrt list-of-numbers))

This programming style is clear and elegant, but unfortunately results in slow code. There are two reasons why:

An example of an iterative version of the same code:

(do ((num list-of-numbers (cdr num))
     (sum 0 (+ (sqrt (car num)) sum)))
    ((null num) sum))

See sections variable-type-inference and let-optimization for a discussion of the interactions of iteration constructs with type inference and variable optimization. Also, section local-tail-recursion discusses an applicative style of iteration.


Next: Trace Files and Disassembly, Previous: Complex Argument Syntax, Up: General Efficiency Hints   [Contents][Index]