SketchyLISP Reference |
Copyright (C) 2006 Nils M Holm |
<<[quotient] | [Index] | [reduce-r]>> |
Conformance: SketchyLISP Core
Purpose: Iterate through a list. Combine the first member of the list with the second member, the result with the third 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 f a default) (letrec ((_reduce (lambda (a res) (cond ((null? a) res) (#t (_reduce (cdr a) (f res (car a)))))))) (cond ((null? a) default) (#t (_reduce (cdr a) (car a))))))
Example:
(reduce cons '(a b c d) #f) => (((a . b) . c) . d)
<<[quotient] | [Index] | [reduce-r]>> |