aleathom
aleathom

Reputation: 23

OCaml Error: Stack overflow during evaluation (looping recursion?)

I have rewritten Scheme code that computes integer log, base 2 in OCaml. Upon compilation, I repeatedly get an error that says "Stack overflow during evaluation (looping recursion?).".

I am a beginner with both of these languages as this is an assignment for one of my classes.

Scheme code:

(define log2
  (lambda (n)
    (if (= n 1) 1 (+ 1 (log2 (quotient n 2))))))

OCaml code:

let rec log2 n = 
  match n with 
  | 1 -> 1 
  | n -> 1 + log2 (n mod 2);;

Upvotes: 1

Views: 156

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66823

The scheme code has quotient and you have rendered this in OCaml with mod. This seems wrong. You want integer division, I would assume, which is / in OCaml.

Upvotes: 1

Related Questions