大家好我已经实现了一个解决方案来查找嵌套列表的平均值。所以我想知道你是否可以想出更好的解决方案或任何可能的错误与我的欢呼声。嵌套列表的平均值
; takes a nested list and return a flattened list
(defun flatten-list (list)
(cond
((null list) list)
((atom list) (list list))
((list (first list))
(append (flatten-list (first list))
(flatten-list (rest list))))
(t
(append (list (first list))
(flatten-list (rest list))))
))
;takes a flattened list and return the sum of the numbers
(defun sum-list (list)
(cond ((null list)
0)
(t
(+ (first list) (sum-list(rest list))))
))
;uses the flatten-list and nested-average to find the average
(defun nested-average (list)
(sum-list (flatten-list list))
(defvar flat-lis)
(setf flat-list (flatten-list list))
(/ (sum-list flat-list) (length flat-list)
))
你尝试过吗?你用输入来测试它吗?你编译它了吗? –
当然,我做了,它正在编译和运行我所有的测试,但我是新来的lisp,我正在寻找新的更优雅的解决方案。 –
这个问题属于http://codereview.stackexchange.com – finnw