只需编写一个递归变量函数,打印列表中的元素,每次调用一个。第一次尝试:
(defn f1 [head & tail]
(when-not (nil? head)
(println head )
(f1 tail) ;; how to unbox/destructure tail ????
) ;; when
) ;; defn
(f1 "one" "two" "three" "four")
但只要得到:-(
one
(two three four)
然后,以一种非常“不优雅”的方式解决了这个问题:
(defn f2Aux [all]
(when-not (empty? all)
(println (first all) )
(f2Aux (rest all))
)
) ; defn
(defn f2 [head & tail]
(f2Aux (list* head tail))
) ;; defn
(f2 "one" "two" "three" "four")
我很确定还有更好的路要走。
谢谢
编辑
。仍在寻找不同于:
(defn f3
;; if we get a list
([all]
(when-not (empty? all)
(println (first all) )
(f3 (rest all))
) ; when
)
;; if we get several args
([head & tail]
(f3 (list* head tail)) ;; pack the args and call us again
)
) ;; defn