Adam
Adam

Reputation: 668

Unbound value in recursive function (Beginner OCaml)

Code:

let isPrime x =
    let checkZero d = match (x mod d, x mod d + 2, intRoot x < d) with
        | (0,_,_)    -> false
        | (_,0,_)    -> false
        | (_,_,true) -> true
        | _          -> checkZero (d + 6) in
    match x with
        | 0 -> false
        | 1 -> true
        | 2 -> true
        | 3 -> true
        | _ -> match (x mod 2, x mod 3) with
            | (0,_) -> false
            | (_,0) -> false
            | _     -> checkZero 5

Error:

line 9, characters 24-33:
Error: Unbound value checkZero

Which refers to the recursive call checkZero (d+6)

I've tried placing the checkZero function as a let ... in in the final checkZero 5 call and added/removed the x parameter in the checkZero function in case there was an error with the definition.

(Running OCaml downloaded in the past week on OSX through homebrew)

Upvotes: 0

Views: 148

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66823

If you want a function to be able to call itself you need to declare it as recursive:

let rec checkZero d ...

Upvotes: 2

Related Questions