Last active
December 11, 2015 20:29
-
-
Save dce/4656124 to your computer and use it in GitHub Desktop.
Better implementation of `fold`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define fold | |
(lambda (l result fun) | |
(cond | |
((null? l) result) | |
(else | |
(fold (cdr l) (fun result (car l)) fun))))) | |
(define fold | |
(lambda (l init fun) | |
(cond | |
((null? (cdr l)) (fun init (car l))) | |
(else | |
(fun (fold (cdr l) init fun) (car l)))))) | |
(define sum | |
(lambda (l) | |
(fold l 0 (lambda (col x) (+ x col))))) | |
(define map | |
(lambda (l fun) | |
(fold l '() (lambda (col x) (cons (fun x) col))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment