私は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)