Reputation: 63
This is a homework. OCaml seems to be made by a psychopath.
let prime : int -> bool
= fun n ->
if n > 2 then
let a = n - 1 in
let rec divisor n a =
if a > 1 && n mod a = 0 then false
else if a = 2 && n mod a <> 0 then true
else divisor n a-1 ;;
else if n = 2 then true
else if n = 1 then false
I am not good at coding and I know that my isPrime
algorithm is wrong.
But I wonder where in my code is the mistake that produces the syntax error.
Also is there any way to define the isPrime
function in a recursive form?
Example:
let rec prime n = ~
Upvotes: 2
Views: 1452
Reputation: 66813
You'll get better responses from experts if you don't gratuitously insult their language :-) But I'm an easygoing guy, so I'll take a stab at your syntax error.
There are quite a few problems in this code. Here are 3 that I see right off:
The symbol ;;
is used to tell the interpreter that you've entered a full expression that you want it to evaluate. It's definitely out of place in the middle of a function declaration.
Your second let
doesn't have an associated in
. Every let
must have an in
after it. The only exception is for defining values at the top level of a module (like your prime
function).
The expression divisor n a-1
is parsed as (divisor n a) - 1
. You want parentheses like this: divisor a (n - 1)
.
Upvotes: 4