chickadee » srfi-116 » ireduce

ireduce f ridentity ilistprocedure

ireduce is a variant of ifold.

ridentity should be a "right identity" of the procedure f — that is, for any value x acceptable to f,

(f x ridentity) ;=> x

ireduce has the following definition: If ilist is (), return ridentity; Otherwise, return (ifold f (icar ilist) (icdr ilist)). In other words, we compute (ifold f ridentity ilist).

Note that ridentity is used only in the empty-list case. You typically use ireduce when applying f is expensive and you'd like to avoid the extra application incurred when ifold applies f to the head of ilist and the identity value, redundantly producing the same value passed in to f. For example, if f involves searching a file directory or performing a database query, this can be significant. In general, however, ifold is useful in many contexts where ireduce is not (consider the examples given in the ifold definition -- only one of the five folds uses a function with a right identity. The other four may not be performed with ireduce).

;; take the max of an ilist of non-negative integers.
(ireduce max 0 nums) ; i.e., (iapply max 0 nums)