0

Curiosity: I was writing codes in Learn Ocaml and when I compiled my codes, the compiler says: "Out of stack error". I guess This is due to the amount of codes I wrote. So I'm wondering how can I check how full is the stack? I wrote about 450-500 lines of codes. With couple of new lines. It could also be the number of codes that's been executed in the stack is taking up all the stack. So I just want to know what cause the problem and how to resolve it? Here is a picture showing the error:

enter image description here

The error went away when I copy only part of the codes in the editor to the toplevel.

1 Answer 1

2

The stack utilization doesn't depend on the size of your program or on the number of lines in it, it depends on the runtime behavior of your program, mostly due to looping recursion. For example, this small function will consume a stack of any size:

let rec f () = f () + f ()

In the picture, that you've provided, I can see at least one case of unbound recursion (i.e., a recursion that definitely will not terminate). When you call insert you're applying it to the whole tree m, instead of a subtree. So at each new invocation nothing actually changes, and you're getting an infinite loop.

Sign up to request clarification or add additional context in comments.

2 Comments

While I agree that the problem is probably a run-away recursion (unless the example trees are really really big) I don't see the problem in insert. The m isn't the tree but the element to be inserted. n is the tree and is reduced.
Meaningful names would make that easier to figure out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.