2

以下代码有什么问题: func 表达式获取包含可以包含表达式的术语的表达式...

(defn term[]
    (def mytmp (zip/xml-zip {:tag :term}))
    (cond 
      (= (first(:content(first vecTok))) "(")
        (do
          (def mytmp (popVecTo mytmp))
          (def mytmp (zip/append-child mytmp (expression)))
          (def mytmp (popVecTo mytmp)))
      :else 
          (def mytmp (popVecTo mytmp)))
    (zip/node mytmp))
(defn expression[]
    (def mytmp (zip/xml-zip {:tag :expression}))
    (def mytmp (zip/append-child mytmp (term)))
    (while (contains? #{"+", "-", "*","/", "&", "|", "<", ">", "="} (first(:content(first vecTok))) )
      (do
        (def mytmp (popVecTo mytmp))
        (def mytmp (zip/append-child mytmp (term)))))
    (zip/node mytmp))
(def vecTok (vec (:content(first(xml-seq (parse  "C:/Users/User/Desktop/forHekronot/BallT.xml"))))))

在文件中:

<a><symbol>(</symbol><identifier>dy</identifier><symbol>-</symbol><identifier>dx</identifier><symbol>)</symbol></a>
4

1 回答 1

1

尽管@jszakmeister 对如何更好地解决问题发表了评论,但让我尝试给出并回答这个问题:

你可以先(def expression)然后再(defn term [] ...)最后(defn expression [] ...)

间接递归的经典例子当然是穷人对正数的奇/偶函数:

clojurec.core=> (def even)
#'clojurec.core/even
clojurec.core=> (defn odd [x]  (and (not (= x 0)) (even (dec x))))
#'clojurec.core/odd
clojurec.core=> (defn even [x] (or (= x 0) (odd (dec x))))
#'clojurec.core/even
clojurec.core=> (even 10)
true 
clojurec.core=> (odd 10)
false
clojurec.core=> (odd 10000)
StackOverflowError   clojure.lang.Numbers.equal (Numbers.java:214)

糟糕,根据文件的大小(或更确切地说是深度),这可能是个问题。但并非全部丢失,我们甚至可以重新定义以在trampoline内部使用:

(defn even [n] 
  (letfn [(evenrec [x] (or (= x 0) #(oddrec (dec x)))) 
          (oddrec  [x] (and (not (= x 0)) #(evenrec (dec x))))]
    (trampoline evenrec n)))
于 2012-10-01T19:00:29.847 回答