将数组移位k的函数语法错误 [英] Syntax error in function to shift array by k
问题描述
我正尝试将数组k
向左移动.
这是我的代码.但是我在shifted;;
行上遇到了编译错误.
I'm trying to shift array by k
to left.
Here's my code. But I'm getting compile error on shifted;;
line.
let shift_left (arr: array) (kk: int) =
let size = Array.length arr in
let k = kk mod size in
let shifted = Array.make size 0 in
for i = 0 to size - 1 do
if i < k
then (shifted.(size - k + i) <- arr.(i))
else (shifted.(i-k) <- arr.(i))
done
shifted;;
let arr = [| 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 |];;
let shifted = shift arr 4;;
Array.iter print_int arr;
print_string "\n";;
Array.iter print_int shifted;
print_string "\n";;
这是我进入终端的内容:
Here is what I getting in terminal:
$ ocamlc -o shift shift.ml
File "shift.ml", line 11, characters 1-8:
Error: Syntax error
推荐答案
此处有两个编译器错误:
There are two compiler errors here:
-
done
和shifted
之间的语法错误.由于换行符在OCaml中不重要,因此将其解析为done shifted;;
,它看起来像函数应用程序,但由于done
是关键字而不是可能引用函数的标识符,因此无效.使用序列运算符;
可以按顺序求值两个表达式.
A syntax error between
done
andshifted
. Because line breaks are not significant in OCaml, it will be parsed asdone shifted;;
, which looks like a function application, but is not valid sincedone
is a keyword, not an identifier that might refer to a function. Use the sequence operator,;
, to evaluate two expressions in sequence.
类型错误:array
不是具体类型,它使用一个类型参数来指定其包含的值的类型.在这种情况下,它应该是int array
.
A type error: array
is not a concrete type, it takes a type parameter specifying the type of value it contains. It should be int array
in this case.
这篇关于将数组移位k的函数语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!