SketchyLISP Reference |
Copyright (C) 2006 Nils M Holm |
<<[reduce] | [Index] | [remainder]>> |
Conformance: SketchyLISP Core
Purpose: Iterate through a list. Combine the second-to-last member of the list with the last member, the result with the third-to-last member, etc. When the given list is empty, return a default.
Arguments:
F - combining function
A - list to reduce
DEFAULT - expr to return when A=()
Implementation:
(define (reduce-r f a default) (letrec ((_reduce (lambda (a res) (cond ((null? a) res) (#t (_reduce (cdr a) (f (car a) res))))))) (cond ((null? a) default) (#t (let ((ra (reverse a))) (_reduce (cdr ra) (car ra)))))))
Example:
(reduce-r cons '(a b c d) #f) => (a b c . d)
<<[reduce] | [Index] | [remainder]>> |