Created
November 3, 2012 20:29
-
-
Save lispm/4008619 to your computer and use it in GitHub Desktop.
average of a tree
This file contains 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
(defun average (list) | |
(/ (tree-reduce #'+ list) | |
(tree-reduce #'+ list :key (constantly 1)))) | |
(defun tree-reduce (fn tree &key (key #'identity)) | |
(reduce fn tree | |
:key (lambda (item) | |
(if (consp item) | |
(tree-reduce fn item :key key) | |
(funcall key item))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment