3
我正在研究koan的解決方案。我難以理解爲什麼我的解決方案不起作用,但使用comp
的定義確實有效。當我看的comp的定義,我看到:函數組合失敗
(defn comp
"Takes a set of functions and returns a fn that is the composition
of those fns. The returned fn takes a variable number of args,
applies the rightmost of fns to the args, the next
fn (right-to-left) to the result, etc."
{:added "1.0"}
([f] f)
([f g]
(fn
([] (f (g)))
([x] (f (g x)))
([x y] (f (g x y)))
([x y z] (f (g x y z)))
([x y z & args] (f (apply g x y z args)))))
([f g h]
(fn
([] (f (g (h))))
([x] (f (g (h x))))
([x y] (f (g (h x y))))
([x y z] (f (g (h x y z))))
([x y z & args] (f (g (apply h x y z args))))))
([f1 f2 f3 & fs]
(let [fs (reverse (list* f1 f2 f3 fs))]
(fn [& args]
(loop [ret (apply (first fs) args) fs (next fs)]
(if fs
(recur ((first fs) ret) (next fs))
ret))))))
而我的解決方案很相似:
(defn mycomp
([f] f)
([f1 f2]
(fn
([] (f1 (f2)))
([a] (f1 (f2 a)))
([a & more] (f1 (apply f2 a more)))
)
)
([f1 f2 & fs]
(let [fxns (reverse (list f1 f2 fs))]
(fn [& args]
(loop [ret (apply (first fxns) args) fxns (next fxns)]
(if fxns
(recur ((first fxns) ret) (next fxns))
ret))))))
兩個,從我可以告訴之間最大的區別,是第一定義包括三個或更多功能,而第二個定義組成兩個或更多個功能。
請指出我的定義有什麼不正確。
感謝您指出'list *'的東西,我不明白'list'和'list *'之間有區別。另外,我使用零個或多個參數重新實現了我的解決方案,這使得代碼更易於理解。再次感謝! – Davidann