Remembering the tail call of optimized recursive functions in F #

Possible duplicate:
Combine memoization and tail-recursion

So, the following code that I wrote, tail call is optimized using the accumulation variable

let rec counter init count = 
    if init = 1 then count + 1 else
    match init with
    | Even value -> (counter (value/2)  (1 + count))
    | Odd value -> (counter ((3 * value) + 1) (count+1))

let SeqBuilder (initval:int) : int =
    counter initval 0

How do I remember this? The problem I encountered when I tried to remember that a recursive call should go to a memoize object, so should you have a recursive object?

Or is it much easier and am I just inexperienced?

+1
source share
1 answer

F # (, , ), , memoize2 memoization ( ), counter), :

let rec counter = memoize2 (fun init count ->
  if init = 1 then count + 1 else 
  match init with 
  | Even value -> (counter (value/2) (1 + count)) 
  | Odd value -> (counter ((3 * value) + 1) (count+1)) )

, , , F # . FS0040, , ( , ), , , ). , #nowarn "40".

+3

All Articles