问题描述
我正在尝试实现此算法,但是在第12行上却不断出现语法错误,但是我无法查明是什么原因引起的.我是ocaml的新手,我们将不胜感激.
I'm trying to implement this algorithm but I keep getting a syntax error on the 12th line but I cannot pinpoint what is causing it. I'm new to ocaml and any help would be greatly appreciated.
"要通过Eratosthenes的方法查找所有小于或等于给定整数n的素数:
"To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
创建一个从2到n的连续整数列表:(2,3,4,...,n).最初,让p等于2,第一个素数.从p开始,通过以p的增量计数到n来枚举其倍数,并在列表中标记它们(它们将是2p,3p,4p ...;不应标记p本身).在未标记的列表中找到大于p的第一个数字.如果没有这样的号码,请停止.否则,让p现在等于这个新数字(这是下一个质数),然后从步骤3开始重复."
Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).Initially, let p equal 2, the first prime number.Starting from p, enumerate its multiples by counting to n in increments of p, and mark them in the list (these will be 2p, 3p, 4p, ... ; the p itself should not be marked).Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3."
let prime(n) =
let arr = Array.create n false in
let set_marks (arr , n , prime ) = Array.set arr (n*prime) true in
for i = 2 to n do
set_marks(arr,i,2) done
let findNextPrimeNumberThatIsNotMarked (arr, prime , index ) =
let nextPrime = Array.get arr index in
let findNextPrimeNumberThatIsNotMarkedHelper (arr, prime, index) =
if nextPrime > prime then nextPrime
else prime in
;;
推荐答案
添加到Jeffrey的答案中,
Adding to Jeffrey's answer,
我已经在"在这里?,
您现在绝对需要做的是安装并使用适当的OCaml压痕工具以及自动压痕线.意外的自动缩进结果通常表示语法错误,例如忘记;
.没有这样的工具,即使是有才华的OCaml程序员也很难编写没有语法错误的OCaml代码.
What you absolutely need to do right now is to install and use a proper OCaml indentation tool, and auto-indent lines. Unexpected auto-indent results often indicate syntactic mistakes like forgetting ;
. Without such tools, it is very hard even for talented OCaml programmers to write OCaml code without syntax errors.
有很多用于OCaml的自动压头:
There are bunch of auto indenters for OCaml available:
- Emacs和Vim的ocp-indent https://github.com/OCamlPro/ocp-indent
- Emacs的Caml模式和Tuareg模式
- Vim应该还有其他压头,但我不知道...
这篇关于是什么导致语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!