题目终于想通了,贴代码吧,代码只做了浅层数据结构测试,不是很确定更深层次的数据会不会也是成功的。
(defn make-mobile [left right]
(list left right)
)
(defn make-branch [length structure]
(list length structure)
)
(defn left-branch [mobile] (first mobile))
(defn right-branch [mobile] (first (rest mobile)) )
(defn branch-lenght[branch]
(if (seq? (first branch)) nil
(first branch
))
)
(defn branch-structure[branch]
(first (rest branch))
)
(defn mobile? [mobile]
(if (seq? (first mobile)) true false)
)
(defn total-weight [mobile]
(cond (not (seq? mobile)) mobile
(mobile? mobile) (+ (total-weight (left-branch mobile)