What is causing the syntax error here?

限于喜欢 提交于 2019-12-25 04:26:49

问题


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.

"To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:

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       

    ;;  

回答1:


Adding to Jeffrey's answer,

As I have already answered to you at " What exactly is the syntax error here? ",

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.

There are bunch of auto indenters for OCaml available:

  • ocp-indent for Emacs and Vim https://github.com/OCamlPro/ocp-indent
  • Caml mode and Tuareg mode for Emacs
  • Vim should have some other indenters but I do not know...



回答2:


OCaml has an expression let a = b in c. Your code ends with in, but where is c? It looks like maybe you should just remove the in at the end.

Looking more closely I see there are more problems than this, sorry.

A function in OCaml is going to look like this roughly:

let f x =
    let a = b in
    let c = d in
    val

Your definition for prime looks exactly like this, except that it ends at the for loop, i.e., with the keyword done.

The rest of the code forms a second, independent, function definition. It has a form like this:

let f x =
    let a = b in
    let g x = expr in

The syntactic problem is that you're missing an expression after in.

However, your use of indentation suggests you aren't trying to define two different functions. If this is true, you need to rework your code somewhat.

One thing that may be useful (for imperative style programming) is that you can write expr1; expr2 to evaluate two expressions one after the other.



来源:https://stackoverflow.com/questions/28688733/what-is-causing-the-syntax-error-here

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!