用Clojure语法重写Java if语句 [英] Rewriting java if statements in clojure syntax
本文介绍了用Clojure语法重写Java if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组用Java编写的递归if语句,我尝试将其用于Clojure语法,但收到ArrayOutOfBound错误。该函数接受row = 0,col = 0和多维字符数组。
I have a set of recursive if statements written in Java and I tried taking it to clojure syntax but got the ArrayOutOfBound error. The functions accepts row = 0, col = 0 and a multidimensional array of characters.
这是Java代码:
public static boolean solveMaze(int r, int c, char[][] maze) {
//check for boundaries
if(r < 0 || c < 0 || r >= maze.length || c >= maze[0].length)
return false;
//base case, did i reach the finish?
if(maze[r][c] == '@')
return true;
//check if I'm at a valid point
if(maze[r][c] != '-')
//I have hit a wall, not a valid solution
return false;
//must be on a path, may be the right path
//leave a bread crumb
maze[r][c] = '!';
//check above
if(solveMaze(r-1,c, maze)) {
maze[r][c] = '+';
return true;
}
//check below
if(solveMaze(r+1,c, maze)) {
maze[r][c] = '+';
return true;
}
//check left
if(solveMaze(r,c-1, maze)) {
maze[r][c] = '+';
return true;
}
//check right
if(solveMaze(r,c+1, maze)) {
maze[r][c] = '+';
return true;
}
//if I reach this point...
return false;
}
这是我在clojure中的代码,它提供索引超出范围的错误:
This is my code in clojure that provides the Index out of bound error:
(defn solveMaze [r c m]
(def tt true)
;(def che false)
;check to verify boundaries
(if (or (or (or (< r 0) (< c 0)) (>= r (count m))) (>= c (count (str (m 0)))))
false
true
)
;Check to know if the @ symbol has been found
(if (= (get-in m[r c]) "@")
true
false
)
;Checking if current index is a valid one
(if (not= (get-in m[r c]) "-")
;Wall hit
false
true
)
;found a path, leave a breadcrumb
(def temp_map (assoc-in m[r c] "!"))
;Check North
(if (= (solveMaze (dec r) c m) true)
(def temp_map (assoc-in m [r c] "+"))
true
;false
)
;Check South
(if (= (solveMaze (inc r) c m) true)
(def temp_map (assoc-in m[r c] "+"))
true
;false
)
;Check East
(if (= (solveMaze r (dec c) m) true)
(def temp_map (assoc-in m[r c] "+"))
true
;false
)
;Check West
(if (= (solveMaze r (inc c) m) true)
(def temp_map (assoc-in m[r c] "+"))
true
;false
)
;If code gets here
(= tt false)
)
请问我在做错什么吗???我已经尝试过使用clojure的多个if构造,但是它不起作用。
Please is there something I am doing wrongly??? I have tried several if constructs using clojure but it doesn't work.
推荐答案
您需要阅读Clojure的基础知识:
You need to read up on the basics of Clojure:
- 勇敢的Clojure (在线图书)
- 获取Clojure 图书
- Clojure CheatSheet 书
- Brave Clojure (online & book)
- Getting Clojure book
- Clojure CheatSheet book
这篇关于用Clojure语法重写Java if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文