简体   繁体   中英

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.

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

I can explain in iter a 0 .

The definition of a recursive local function in OCaml looks like this:

let rec fun arguments = definition in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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