conj 在通过 recur 调用时不添加到向量的末尾
conj not adding to end of vector when called via recur
为什么这老是问杰克要不要喝杯茶,而不是其他爸爸。
(defn tea-anyone
"Ask 'fathers' if they would like some tea"
[fathers]
(loop [asks 0 to-ask fathers]
(let [[father & others] to-ask]
(println (str "Cup of tea " father "? "))
(if (> asks 6)
(println (str father ": All right then!"))
(recur (inc asks) (conj others father))))))
(tea-anyone ["Jack" "Ted" "Dougle"])
因为 others
不是向量。自己看看:
(let [[f & o :as a] [1 2 3 4]]
(println f)
(println o)
(println a)
(println (type f))
(println (type o))
(println (type a))
(println (type (conj o 5)))
(println (type (conj a 5))))
为了达到你想要的效果,你可以使用cycle
。
试试这个:
(recur (inc asks) (conj (vec others) father))
为什么这老是问杰克要不要喝杯茶,而不是其他爸爸。
(defn tea-anyone
"Ask 'fathers' if they would like some tea"
[fathers]
(loop [asks 0 to-ask fathers]
(let [[father & others] to-ask]
(println (str "Cup of tea " father "? "))
(if (> asks 6)
(println (str father ": All right then!"))
(recur (inc asks) (conj others father))))))
(tea-anyone ["Jack" "Ted" "Dougle"])
因为 others
不是向量。自己看看:
(let [[f & o :as a] [1 2 3 4]]
(println f)
(println o)
(println a)
(println (type f))
(println (type o))
(println (type a))
(println (type (conj o 5)))
(println (type (conj a 5))))
为了达到你想要的效果,你可以使用cycle
。
试试这个:
(recur (inc asks) (conj (vec others) father))