[英]4clojure #58 (compose)… stuck
嘗試寫作(#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語句使用與函數體相同的代碼確切地顯示了我期望的結果。
我認為您只是將paren放在錯誤的位置。 這有效:
(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
返回nil。 我經常將探測功能用於這種調試:
(defn probe-on
([x]
(println x)
x)
([x msg]
(println msg x)
x))
,但您現在可以(或即將推出,但它們仍然有些新),可以獲取不需要您使用此類調試技術的編輯器/調試器。 參見proto-REPL和Sayid。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.