Recursion 在clojure中递归无限,不返回值

Recursion 在clojure中递归无限,不返回值,recursion,clojure,clojurescript,maze,Recursion,Clojure,Clojurescript,Maze,我对clojure不熟悉。在这里您可以看到函数fp_fun。我正在使用这个函数,它将变为无穷大。我认为它没有返回值。请告诉我答案。提前谢谢 (defn fp\u fun ([x y行列向量] (如果(或(=y列)) 0) (如果(=向量[xy]“@”) 1) (如果(=vec[xy]“#”) 0) (def x1(+x1)) (def y2(-y 1)) (如果(((fp_fun x y2行列向量))1) 1) (如果(((fp_fun x1 y行列向量))1) 1) 1) (println“!

我对clojure不熟悉。在这里您可以看到函数fp_fun。我正在使用这个函数,它将变为无穷大。我认为它没有返回值。请告诉我答案。提前谢谢

(defn fp\u fun
([x y行列向量]
(如果(或(=x行)(>=y列))
0)
(如果(=向量[xy]“@”)
1)
(如果(=vec[xy]“#”)
0)
(def x1(+x1))
(def y2(-y 1))
(如果(((fp_fun x y2行列向量))1)
1)
(如果(((fp_fun x1 y行列向量))1)
1)
1)
(println“!”x y)
0
) )

首先,我将应用emacs自动格式化程序,然后让我们研究一下这里每个表达式的含义

(defn fp_fun
  ([x y row col vec]
   (if (or (< x 0)
           (< y 0)
           (>= x row)
           (>= y col))
     0)
   (if (= vec[x y] "@")
     1)
   (if (= vec[x y] "#")
     0)
   (def x1 (+ x 1))
   (def y2 (- y 1))
   (if (= ( (fp_fun x y2 row col vec)) 1  )
     1)
   (if (= ( (fp_fun x1 y row col vec)) 1  )
     1)
   1)
  (println "!" x y)
  0) )
第二种形式是if,当有多种方法使用不同数量的参数调用此函数时

(defn
  ([one-arg]
   result-expression-here)
  ([fist-arg second-arg]
   other-result-expression-here))
您所拥有的是两者的一部分,因此让我们删除最后两个表达式:

 (println "!" x y)

这就给我们留下了

(defn fp_fun
  ;; this function always takes 5 args, so prefer the basic form
  ([x y row col vec]

   ;; this first expression does absolutly nothing and it totally ignored
   (if (or (< x 0)
           (< y 0)
           (>= x row)
           (>= y col))
     0)

   ;; this next expression is also ignored, only the last value in a function determines it's result
   (if (= vec[x y] "@")
     1)

   ;; so this one does nothing as well
   (if (= vec[x y] "#")
     0)

   ;; these two define some global variables
   ;; using def inside a function is almost always better done with a let expression
   (def x1 (+ x 1))
   (def y2 (- y 1))

   ;; this will always run because none of the above statements cause the function to
   ;; stop and return it's value early.
   (if (= ( (fp_fun x y2 row col vec)) 1  )
     1)

   ;; and  then the stack will overflow before we get here
   (if (= ( (fp_fun x1 y row col vec)) 1  )
     1)
   1))

您好,谢谢您的回答,但如果我添加4条if语句,它再次进入for循环会怎么样。你能告诉我吗<代码>(defn find_path[xy row col board](if(或(=x row)(>=y col))0(if(=(str(get in board[xy]))“@”)1(if(=(str(get in board[xy]))“#”)0(if(=(find_path x(-y1)row col board)1)1(if(=(find_path(+x1 y row col board))1)1(如果(和(>=x0)(还有一件事,我仍然有同样的问题。如果我再加上一个条件,它就会进入有限循环,就是你第一次给我的解。最后,如果我添加另一个表达式,它将进入无限循环。你现在给我的解决方案也是一样
0
(defn fp_fun
  ;; this function always takes 5 args, so prefer the basic form
  ([x y row col vec]

   ;; this first expression does absolutly nothing and it totally ignored
   (if (or (< x 0)
           (< y 0)
           (>= x row)
           (>= y col))
     0)

   ;; this next expression is also ignored, only the last value in a function determines it's result
   (if (= vec[x y] "@")
     1)

   ;; so this one does nothing as well
   (if (= vec[x y] "#")
     0)

   ;; these two define some global variables
   ;; using def inside a function is almost always better done with a let expression
   (def x1 (+ x 1))
   (def y2 (- y 1))

   ;; this will always run because none of the above statements cause the function to
   ;; stop and return it's value early.
   (if (= ( (fp_fun x y2 row col vec)) 1  )
     1)

   ;; and  then the stack will overflow before we get here
   (if (= ( (fp_fun x1 y row col vec)) 1  )
     1)
   1))
 (defn fp_fun [x y row col vec]
   (if (or (< x 0)
           (< y 0)
           (>= x row)
           (>= y col))
     0
     (if (= (vec x y) "@")
       1
       (if (= (vec x y) "#")
         0
         (let [x1 (+ x 1)
               y2 (- y 1)]
           (if (= (fp_fun x y2 row col vec) 1)
             1
             (if (= (fp_fun x1 y row col vec) 1)
               1)))))))
 (cond
   (or (< x 0) (< y 0) (>= x row) (>= y col)) 0
   (= (str(get-in board [x y])) "@")          1
   (= (str (get-in board [x y])) "#")         0
   (= (find_path x (- y 1) row col board) 1)  1
   (= (find_path (+ x 1) y row col board) 1)  1
   (and (>= x 0) (< y col))  (if (= (find_path x (+ y 1) row col board) 1)
                               1
                               0)
   :defautl nil) ;; i'm not sure you intended to default to nil