Arrays 使用ocaml对数组进行排序
我正在做一个相当简单的例子来学习如何使用ocaml作为命令式语言。 我猜我把分号弄乱了,但我在代码中找不到任何错误Arrays 使用ocaml对数组进行排序,arrays,sorting,ocaml,imperative,Arrays,Sorting,Ocaml,Imperative,我正在做一个相当简单的例子来学习如何使用ocaml作为命令式语言。 我猜我把分号弄乱了,但我在代码中找不到任何错误 let sort array = for index = 0 to (Array.length array -1) do let boole = ref false; let pos = ref index; let max = ref array.(index); let p = ref !pos; let m = ref !max;
let sort array =
for index = 0 to (Array.length array -1) do
let boole = ref false;
let pos = ref index;
let max = ref array.(index);
let p = ref !pos;
let m = ref !max;
while !pos <> (Array.lenght array -1 ) do
if array.(!pos) > !max then begin
max := array(!pos);
boole := true;
p := !pos
end
pos := !pos + 1
done;
if (!boole = true) then begin
array.(index) <- max;
array.(pos) <- m
end
done ;;
let排序数组=
对于索引=0到(Array.length数组-1)do
设boole=ref为false;
设pos=ref索引;
设max=ref数组(索引);
让p=ref!销售时点情报系统;
让m=ref!最大值;
虽然位置(数组长度数组-1)do
if数组。(!pos)>!然后开始
最大值:=数组(!位置);
布尔:=真;
p:=!销售时点情报系统
结束
位置:=!职位+1
完成;
如果(!boole=true),则开始
array.(index)代码中的以下修复可能会有所帮助-至少对编译代码有所帮助:
let sort toto =
for index = 0 to (Array.length toto - 1) do
let boole = ref false in
let pos = ref index in
let max = ref toto.(index) in
let p = ref !pos in
let m = ref !max in
begin
while !pos <> (Array.length toto - 1 ) do
begin
if (toto.(!pos) > !max) then
begin
max := toto.(!pos);
boole := true;
p := !pos;
end;
pos := !pos + 1;
end
done;
if (!boole = true) then begin
toto.(index) <- !max;
toto.(!pos) <- !m
end
end
done;;
让我们排序toto=
对于索引=0到(Array.length toto-1)do
将boole=ref设为false
设pos=ref索引为in
设max=ref toto.(索引)in
让p=ref!pos in
让m=ref!最大值
开始
虽然位置(数组长度toto-1)do
开始
如果(toto.(!pos)>!max)那么
开始
最大值:=toto.(!pos);
布尔:=真;
p:=!销售时点情报系统;
结束;
位置:=!pos+1;
结束
完成;
如果(!boole=true),则开始
toto.(index)首先,没有让x=y在OCaml中,正确的语法是
让x=y进入
,而且您不应该忘记取消引用
let sort array =
for index = 0 to (Array.length array -1) do
let boole = ref false in
let pos = ref index in
let max = ref array.(index) in
let p = ref !pos in
let m = ref !max in
while !pos <> (Array.length array -1 ) do
if array.(!pos) > !max then begin
max := array.(!pos);
boole := true;
p := !pos
end;
pos := !pos + 1;
done;
if (!boole = true) then begin
array.(index) <- !max;
array.(!pos) <- !m;
end;
done ;;
let排序数组=
对于索引=0到(Array.length数组-1)do
将boole=ref设为false
设pos=ref索引为in
设max=ref数组(索引)为in
让p=ref!pos in
让m=ref!最大值
虽然位置(数组长度数组-1)do
if数组。(!pos)>!然后开始
最大值:=数组。(!pos);
布尔:=真;
p:=!销售时点情报系统
结束;
位置:=!pos+1;
完成;
如果(!boole=true),则开始
数组。(索引)好的,我不知道我必须在那里使用let-in语法。非常感谢。存在设x=y代码>在顶层,但不是在表达式中。当然,这不是一个表达式,它是一个语句,可以用作顶层。好吧-与其他答案相同的建议-但被否决。。。我想理解。请考虑使用适当的OcAML ID,最好是使用<代码> OCP缩进< /代码>。此外,不需要将这个begin/end
块添加到while
构造中,因为它们已经包含隐式块。第三,像数组
,列表
等名称可以在OCaml中使用,而使用toto
是很难看的?乍一看,我无法识别您使用的算法。@JoeGob我只是在玩ocaml中的命令式编程,我不是在寻找最好的算法
let sort array =
for index = 0 to (Array.length array -1) do
let boole = ref false in
let pos = ref index in
let max = ref array.(index) in
let p = ref !pos in
let m = ref !max in
while !pos <> (Array.length array -1 ) do
if array.(!pos) > !max then begin
max := array.(!pos);
boole := true;
p := !pos
end;
pos := !pos + 1;
done;
if (!boole = true) then begin
array.(index) <- !max;
array.(!pos) <- !m;
end;
done ;;