haskell - How does this memoized fibonacci function work? -


In the current practice assignment of functional programming courses, we've got to create a memoized version of one given For the interpretation of the function of memoanization, the following example has been given:

  fiblist = [fibm x | X & LT; - [0 ..]] FIBM 0 = 0 FIBM 1 = 1 FIBM N = FBL !! (N-1) + Fibliƫlt !! (N-2)   

But I do not fully understand how it works.

Let's call fibm 3 .

  FILM 3 - & gt; FIBIL !! 2+ FB list 1 - & gt; [Fiber 0, Fimboma 1, FB 2] !! 2 + [Fib 0, FIBM 1] !! 1 - & gt; FIMBUM 2 + FIBM1 - & gt; (Fiberlist !! 1 + Fibliast 0) + 1 - & gt; ([FIBM 0, Fib 1] !! 1 + [FIBM 0] !! 0) + 1 - & gt; (FImM + 1FBM) + 1 - & gt; 1 + 0 + 1 - & gt; 2   

With other questions / answers and googling, I have learned that in some way, the evaluation is shared between the FB list.

Does this mean that, for example, fiblist !! 2+ FB list !! 1 , the list value is only calculated once for the fiblist !! 2 and then just reused for fiblist !! 1 ?

Then two Fibonacci numbers are calculated once per call, so there is no unimportant number of calls. But what about the "lower" level of the call in the Fiberlist function? In the original fibm call "text">

The important part here is that the list has been evaluated appropriately, which means that the element is not requested for the first time. Once it has been evaluated, there is more to see it. So in your example, you are saying that the value is calculated only once for the fiblist !! 2 and then reused for fiblist !! 1 .

The 'lower level' of the Fibbles function works the same way. For the first time I call fiblist !! 1 will be evaluated by calling fibm 1 which is only 1, and this value will be in the list. When you try to get a high fibonacci number, then call Fiberlist to FILM , which will see those values ​​in less - and potentially already evaluated Done - fiblist status .

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -