[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: why tail recursion matters and why Java isn't it, was Re: lispperformance was Re: problems with lisp



Pascal Costanza wrote:
> You seem to suggest that recursion is always the most "natural" solution 
> but that's just not true. In many cases, an iteration expresses much 
> more clearly the intention of a programmer. Here are three Common Lisp 
> versions of an example of a collector I repeatedly need in a current 
> project:
> 
> (loop for element in list
>       do collect (car element))
> 
> (defun collect-elements (list)
>   (unless list
>     (cons (caar list) (collect-elements (cdr list)))))
> 
> (reduce 'cons list :key 'car :from-end t :initial-value nil)

Luke Gorrie mailed me this other solution.

(mapcar 'car list)


Pascal

-- 
Pascal Costanza               University of Bonn
mailto:costanza@web.de        Institute of Computer Science III
http://www.pascalcostanza.de  Römerstr. 164, D-53117 Bonn (Germany)