haskell - How to use the memoize function in Data.Function.Memoize -


After failing to create my memorandum, I changed the class to say and tried to use it, Fibonacci sequence :

  Fiber :: int -> Int fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n-2)   

I use the memoize function Tried in many ways in the classroom, but even the construction below seems to be slow (eating 10 seconds on fib 33):

 < Code> fib :: int -> int fiber 0 = 0 fiber 1 = 1 fib en = memoise fib (n - 1) + memoise fib (n -2) fib ':: int - & gt; I have tried to distribute  memoize  in other ways, but it does not appear to improve performance. I know that there are other ways to count this problem more efficiently, but for my basic problem I would like to use the Memoise package, so my question is, in this package,  memoize  How does performance improve with the function?   

Remembering clearly is only useful if you do just once , and then many times after that, in your view, you keep remembering the function over and over again. This idea is not!

  fib 'n = memoize fib n   

is the right thing, yet will not do the desired work for a subtle reason: it is not because it Clearly mentions its logic.

  fib '= memoize fib   

is correct. Now, in order to actually speed up the phib function, you have to referenced back in the remembered version.

  fib n = fib '(n-1) + fib' (n-2)   

You can see: just to be strictly dry Follow and avoid avoiding the boilerplate as much as possible, you will reach the correct version!

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -