Skip to content

Instantly share code, notes, and snippets.

@lispm
Created November 3, 2012 20:29
Show Gist options
  • Save lispm/4008619 to your computer and use it in GitHub Desktop.
Save lispm/4008619 to your computer and use it in GitHub Desktop.
average of a tree
(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