尝试作曲(#58)
(defn compose [& fns]
(fn [a & opts]
(if opts
;println can be removed, just shows my confusion
(reduce (fn [x y] (println(apply y (list x))) (apply y (list x))) (cons a opts) (reverse fns)))
(reduce (fn [x y] (y x)) a (reverse fns))))
((compose rest reverse) 1 2 3 4)我在这件事上浪费了太多时间所以我求助于.我只有2-3天的经验,所以请原谅丑陋的clojure,最有可能是不必要的。我想知道这是什么问题,而不是得到一个完全不同(我相信更好)的答案。
让我困惑的是,错误表明我试图将列表作为函数调用,但是print语句显示了我所期望的结果,使用与函数体相同的代码。
发布于 2017-05-11 03:24:46
我想你把父母弄错地方了。这样做是可行的:
(defn compose [& fns]
(fn [a & opts]
(if opts
(reduce (fn [x y] (apply y (list x))) (cons a opts) (reverse fns))
(reduce (fn [x y] (y x)) a (reverse fns)))))我很容易马上就看到这个问题,因为我使用了一个了解parens和对齐的编辑器--我只是将您的代码放在IntelliJ/Cursive中。草书使用结构编辑(也称为Paredit)。
同样,在调试时,请注意println返回零。在这种调试中,我经常使用探测函数:
(defn probe-on
([x]
(println x)
x)
([x msg]
(println msg x)
x)),但是您现在(或者将来,它们仍然是一个新的)可以获得不需要使用此类调试技术的编辑器/调试器。见和Sayid。
https://stackoverflow.com/questions/43905852
复制相似问题