我想拿一個數字,20,和一個列表。 '(1 2 3 4 5 6 7 8 9 10)
,並返回包含原始列表中每個值的兩個值的集合:原始值與將該值潛入20時的餘數成對。如果原始值以某種方式鍵入餘數,這將很好,這樣我就可以輕鬆地檢索出產生特定餘數的每個數字。基本上我想要一些功能func
:Clojure:在列表中進行復雜迭代?
user=> (func 20 '(1 2 3 4 5 6 7 8 9 10))
'(:0 1, :0 2, :2 3,... :20 0)
不過,我有一個非常困難的時期剛剛搞清楚如何遍歷列表。有人可以幫助我理解如何獨立使用列表元素,然後如何返回20除以的元素,並返回餘數?
我的想法是在計算平方根的程序中使用類似的東西。如果數字是由剩餘鍵,然後我可以查詢集合來獲取與0
這裏是我的打算有關初步的方式其餘將輸入所有數字。
;; My idea on the best way to find a square root is simple.
;; If I want to find the square root of n, divide n in half
;; Then divide our initial number (n) by all numbers in the range 0...n/2
;; Separate out a list of results that only only return a remainder of 0.
;; Then test the results in a comparison to see if the elements of our returned
;; list when squared are equal with the number we want to find a square root of.
;; First I'll develop a function that works with evens and then odds
(defn sqroot-range-high-end [input] (/ input 2))
(sqroot-range-high-end 36) ; 18
(defn make-sqrt-range [input] (range (sqroot-range-high-end (+ 1 input))))
(make-sqrt-range 36) ; '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18)
(defn zero-culler [input] (lazy-seq (remove zero? (make-sqrt-range input))))
(zero-culler 100) ; '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18)
(defn odd-culler [input] (lazy-seq (remove odd? (zero-culler input))))
(odd-culler 100) ; '(2 4 6 8 10 12 14 16 18)
;;the following is where I got stuck
;;I'm new to clojure and programming,
;;and am just trying to learn in a way that I understand
(defn remainder-culler [input]
(if
(/ input (first odd-culler (input)))
input)
(recur (lazy-seq (input)))
)
(remainder-culler 100)
你不需要在lazy-seq中換行,它已經很懶。 – 2014-10-07 18:07:00