在OCaml中找到一个矩阵中的x
问题描述:
我想在OCaml中编写一个函数,给定矩阵中包含条目“u”,“d”,“l”,“r”和“x”的坐标“,返回true如果从该坐标的'方向'结束于”x“,或者否则为。在OCaml中找到一个矩阵中的x
我的策略是检查,如果我之前访问过的当前单元格(如果是的话,返回假),通过传递矩阵的bool的吨通过,更新当前单元格的真值。
我现在的尝试是低于(ñ是我试图浏览样品基质):
let n = [| [|"u"; "d"; "l"; "l"; "r"|];
[|"r"; "d"; "r"; "l"; "l"|];
[|"u"; "r"; "d"; "d"; "l"|];
[|"u"; "l"; "l"; "d"; "u"|];
[|"r"; "x"; "l"; "l"; "d"|];
[|"r"; "d"; "u"; "d"; "l"|]|];;
let xMax = (Array.length n);;
let yMax = (Array.length n.(0));;
let t = Array.make_matrix xMax yMax false;;
let rec f m x y t =
if x < 0 || y < 0 || x >= (Array.length m) || y >= (Array.length m.(0)) || t.(x).(y) = true
then false
else (
if m.(x).(y) = "x" then true
else (
t.(x).(y) <- true
if m.(x).(y) = "l" then f m x (y-1) t
else if m.(x).(y) = "r" then f m x (y+1) t
else if m.(x).(y) = "u" then f m (x-1) y t
else if m.(x).(y) = "d" then f m (x+1) y t
else false
)
);;
任何人都可以用如何解决这个帮助吗?这是行
t.(x).(y) <- true
不工作(注释它使功能运行,但不更新矩阵t)。
在此先感谢!
答
您错过了t.(x).(y) <- true
表达式后的分号。如果没有分号,程序会被编译器解析为:
t.(x).(y) <- (true if m.(x).(y) = "l" then f m x (y-1) t ...)
Add';' '真'后,或不会编译。 – avysk
您应该始终指定遇到的失败类型:编译时(语法错误,键入错误)或运行时错误。最佳做法是复制系统的确切错误信息以便使用,其他人更容易帮助您 – ghilesZ
谢谢。下次我会记住这一点 – Ronnie268