我只是接觸到Clojure,並想做一些簡單的功能讓我的腳溼潤,並嘗試擺脫我的命令性編程背景。在Clojure中,我怎樣才能正確地終止這個循環
這下面只是搜索一個字符串重複元素,但循環似乎旋轉失控;我做錯了什麼?是的,這可能看起來勢在必行;我仍然試圖圍繞函數式編程和非狀態變化進行討論。是的,沒有驗證的HTML,我只是用它作爲一個例子。
(def src "
<html>
<head>
<title>Astronomy
</title>
<title>Science
</title>
</head>
</html>
")
(defn findit [ele body i]
(let [n (.indexOf body (clojure.string/join ["<" ele ">"]) i)]
(if (> n -1)
(let [n2 (.indexOf body (clojure.string/join ["</" ele ">"]) (+ n 1))] ;if we found opening element, then find closing element
(if (> n2 -1) ;found both open and close of element?
[(subs body (+ n (.length ele) 2) n2) (+ n2 1)]))))
)
; (let [[r i] (findit "title" src 69)] (println i) (if r (println r,i)))
(defn findthem [ele body]
(loop [x 0]
(let [[r i] (findit ele body 0)]
(println r,i)
(if i
(recur i)))
)
)
(findthem "title" src)
它可以幫助你來試試這個:(環路[X 0](的println 「你好」)(讓[我42](如果我(復發I)))) – user100464 2013-03-18 14:46:31
會導致無限循環,正確?它不會是(重複( - 我1))或類似的東西,以幫助它減少和停止循環? – scape 2013-03-18 15:33:14