maya
maya

Reputation: 159

syntax error in the end of program

I am trying to compile this code and it is returning a syntax error in the last line:(, have any one idea why

let nth_index str c n = 
let i = 0 in 
    let rec loop n i=
    let j = String.index_from str i c in
        if n>1 then loop (n-1) j 
        else    
            j

Upvotes: 0

Views: 492

Answers (2)

trivelt
trivelt

Reputation: 1965

So, according to the @Jeffrey's response, you can write:

let nth_index str c n = 
    let rec loop n i =
        let j = String.index_from str i c in
        if n>1 then loop (n-1) j else j
    in loop n 0

But looking at your algorithm, probably you wanted to write if n>1 then loop (n-1) (j+1) else j in penultimate line. If yes, please note that this method can raise exception, so good practice is to name it nth_index_exn.

Upvotes: 2

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66793

Your outermost let doesn't require a matching in. It makes a top-level definition.

However, the other three lets do require a matching in. And you only have two ins.

Upvotes: 3

Related Questions