Reputation: 11
This is a principal code in OCaml, which is for Sigma function with Tail recursion.
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
Does anyone help me to fill up <A>
to `... ?
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
Upvotes: 1
Views: 273
Reputation: 66803
I can explain in iter a 0
.
The definition of a recursive local function in OCaml looks like this:
let rec
fun arguments=
definitionin
expression
In the expression you can call the function fun, which is defined by its definition.
The skeleton definition of sigma_tail_rec
is using a local recursive function named iter
that takes two arguments. And the expression (giving the overall value of sigma_tail_rec
) is 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). So there is a lot to work out, I'm afraid.
Upvotes: 1