[英]How can I make Tail recursion function with OCaml? Syntax advice & explanation needed
This is a principal code in OCaml, which is for Sigma function with Tail recursion. 这是OCaml中的主要代码,适用于具有Tail递归的Sigma函数。
let sigma_tail_rec term a next b =
let rec iter a result =
if "A" then "B"
else iter "C" "D"
in iter a 0
There I don't know how to fill <A>
to <D>
and therefore can't compile 我不知道如何将
<A>
填充为<D>
,因此无法编译
Does anyone help me to fill up <A>
to `... ? 有没有人帮我填了
<A>
到`...?
I've tried like this 我已经试过了
let sigma_tail_rec term a next b =
let rec iter a result =
if a>b then result+1
else iter a+1 result+a
in iter a 0
I don't know what " in iter a 0
" syntax means 我不知道“
in iter a 0
”语法的含义
I can explain in iter a 0
. 我可以
in iter a 0
来解释。
The definition of a recursive local function in OCaml looks like this: OCaml中的递归局部函数的定义如下所示:
let rec
fun arguments=
definitionin
expressionlet rec
乐趣 参数=
定义in
表达
In the expression you can call the function fun , which is defined by its definition . 在表达式中,您可以调用函数fun ,它由其定义定义 。
The skeleton definition of sigma_tail_rec
is using a local recursive function named iter
that takes two arguments. sigma_tail_rec
的框架定义使用名为iter
的本地递归函数,该函数带有两个参数。 And the expression (giving the overall value of sigma_tail_rec
) is iter a 0
. 表达式 (给出
sigma_tail_rec
)为iter a 0
。
It's hard to say more without more information about this assignment. 如果没有有关此作业的更多信息,很难说更多。 Generally speaking, I'd say you need to call
term
(which probably decides when you've reached the end of the calculation) and next
(which possibly generates a new value to process). 一般来说,我想说的是,您需要调用
term
(它可能决定您何时到达计算的结尾)和next
(可能会生成要处理的新值)。 So there is a lot to work out, I'm afraid. 恐怕还有很多工作要做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.