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

白昼怎懂夜的黑 提交于 2019-12-11 03:29:32

问题


I have a problem. I need to get average length from this list: (1 (2 3 4) 5 (6 7) 8 (9)). It should be 2. And I have no idea where to start...

I tried to get (1 2 3 4 5 6 7 8 9) from (1 (2 3 4) 5 (6 7) 8 (9)) but I failed, because (reduce #'append list-name) isn't working.

I have idea how to calculate this but I need to get all lists inside (1 (2 3 4) 5 (6 7) 8 (9)) like this:

list1 = (1 5 8)
list2 = (2 3 4)
list3 = (6 7)
list4 = (9)

But I don't know how.

Can u give me some help?


回答1:


(defun nested-lists-average-length (ls &aux (i 0) (n 0))
    (dolist (a ls (float (/ _______)))
      (if (_______ a) 
        (progn (_______ i) 
               (incf n (_______ a) )))))

Fill ... in ... the ... blanks. :)



来源:https://stackoverflow.com/questions/15322716/lisp-how-to-get-the-average-length-for-nested-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!