简体   繁体   English

递归ocaml堆栈溢出

[英]Recursion ocaml stack overflow

I'm trying to draw fractal: for every circle displeyed I want to draw circle half it's size to the left and right of that circle. 我正在尝试绘制分形:对于每个不完整的圆,我都想绘制一个圆,该圆的大小是该圆的左右两边的一半。 Here is my code: 这是我的代码:

let rec drawCircle x y r =
    let halfSize = r/2 in
        draw_circle x y r;
        drawCircle (x+r) y halfSize;
        drawCircle (x-r) y halfSize;;

It compiles but when I run it I get stack overflow. 它可以编译,但是当我运行它时,我会得到堆栈溢出。 The question is why and how can I fix it in this function ? 问题是为什么以及如何在此功能中修复它?

Your code doesn't have any conditions to terminate recursion. 您的代码没有任何条件可以终止递归。

For example, do not call drawCircle again when r == 1 pixel. 例如,当r == 1像素时,不要再次调用drawCircle。 Or any other case on your choice. 或您选择的任何其他情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM