Arc Forumnew | comments | leaders | submitlogin
4 points by simonb 6170 days ago | link | parent

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...