gpt4 book ai didi

list - LISP - 如何从嵌套列表中获取平均长度?一步一步的解释

转载 作者:太空宇宙 更新时间:2023-11-03 19:04:26 24 4
gpt4 key购买 nike

我知道这个答案已得到解答,但我是 LISP 的新手,我不明白!

LISP - How to get the average length for nested lists?

(defun nested-lists-average-length (ls &aux (i 0) (n 0))
(dolist (a ls (float (/ _______))) ;maybe (/ i n) ;;get average lenght of list
(if (_______ a) ;??
(progn (_______ i) ;??
(incf n (_______ a) ))))) ;?

我写了这个函数:

(defun average-sublist-length (list &aux (sum 0) (n 0))
(dolist (x list (when (plusp n) (/ sum n)))
(when (plusp x)
(incf sum (min x 100))
(incf n))))

(average-sublist-length '(1 2 3 4 (5 6 7) 8 9))

如果我想让它执行,我会得到错误:

PLUSP: (5 6) is not a real number Now I've got problems with calling it.

感谢您的帮助!

最佳答案

添加调试信息:

  ....
(dolist (a ls ....)
(print `(:debug :i ,i :n ,n :a ,a))
(if ....)
....

the question you link to 中的示例打印以下内容:

(nested-lists-average-length '(1 (2 3 4) 5 (6 7) 8 (9)))

(:DEBUG :I 0 :N 0 :A 1)
(:DEBUG :I 0 :N 0 :A (2 3 4))
(:DEBUG :I 1 :N 3 :A 5)
(:DEBUG :I 1 :N 3 :A (6 7))
(:DEBUG :I 2 :N 5 :A 8)
(:DEBUG :I 2 :N 5 :A (9))

关于list - LISP - 如何从嵌套列表中获取平均长度?一步一步的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49840671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com