3 points by parenthesis 4282 days ago | link | parent The current reduce is fine if the given function can take 0,1 or 2 arguments. E.g.`````` (reduce + ()) -> 0 (reduce + '(1)) -> 1 `````` But if you were using, e.g.`````` (def add (x y) (+ x y)) `````` then the above cases wouldn't work.But if reduce is redefined thus:`````` (def reduce (f xs) (if (cddr xs) (reduce f (cons (f (car xs) (cadr xs)) (cddr xs))) (cdr xs) (apply f xs) xs (car xs) nil)) `````` Then`````` (reduce add ()) -> nil (reduce add '(1)) -> 1``````
 4 points by simonb 4281 days ago | link A somewhat messy attempt at mimicking CL's reduce:`````` (let init-default (uniq) (def reduce (f xs (o init init-default)) ((afn (xs) (if (cddr xs) (self (cons (f (car xs) (cadr xs)) (cddr xs))) (cdr xs) (apply f xs) xs (car xs) (f))) (if (is init init-default) xs (cons init xs))))) `````` For reference:"In the normal case, the result of reduce is the combined result of function's being applied to successive pairs of elements of sequence. If the subsequence contains exactly one element and no initial-value is given, then that element is returned and function is not called. If the subsequence is empty and an initial-value is given, then the initial-value is returned and function is not called. If the subsequence is empty and no initial-value is given, then the function is called with zero arguments, and reduce returns whatever function does. This is the only case where the function is called with other than two arguments."http://www.lispworks.com/documentation/HyperSpec/Body/f_redu...-----