Ocaml:再次调用递归函数
所以我有一个递归函数,它接受 2 个整数和一个 out_channel ,并且基本上打印行(a,a+1)。它应该这样做直到 a 的值等于 b。即如果 a = 1, b = 5
line(1,2) 线(2,3) ...line(4,5)
> let rec print_line (out:out_channel)(a:int)(b:int) : unit =
if (a < b) then output_string out ("line("^string_of_int(a)^","^string_of_int(a+1)^")\n")
> ;;
我想让它递归,它不断打印行(a,a+1) 直到a 不再小于b。我究竟该如何再次调用它?
任何帮助将不胜感激。
So I have a recursive function that takes in 2 ints, and a out_channel and basically prints line(a,a+1). It should do this until value of a is equal to b. I.e if a = 1, b = 5
line(1,2)
line(2,3)
...line(4,5)
> let rec print_line (out:out_channel)(a:int)(b:int) : unit =
if (a < b) then output_string out ("line("^string_of_int(a)^","^string_of_int(a+1)^")\n")
> ;;
I want to make it recursive where it keeps printing the line(a,a+1) until a is no longer less than b. How exactly do I call it again?
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此:首先检查是否
a >= b
在这种情况下,您已完成并可以返回()
。否则打印一行(就像你所做的那样),然后递归调用你的函数,并增加a
。总而言之:So: first check whether
a >= b
in which case you are done and can return()
. Otherwise print one line (the way you did) followed by recursive call to your function, with incrementeda
. So altogether: