sky0189
sky0189

Reputation: 11

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

Upvotes: 1

Views: 273

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

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 = 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.

Upvotes: 1

Related Questions