大家好,我已经实施了一个解决方案来查找嵌套列表的平均值。所以我想知道你是否能想到一个更好的解决方案或任何可能的错误与我的欢呼。
; 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)
))