Skip to content

Instantly share code, notes, and snippets.

@dce
Last active December 11, 2015 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dce/4656124 to your computer and use it in GitHub Desktop.
Save dce/4656124 to your computer and use it in GitHub Desktop.
Better implementation of `fold`.
(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