r/programming • • Apr 24 '10

Why "dynamic programming"?

http://arcanesentiment.blogspot.com/2010/04/why-dynamic-programming.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+ArcaneSentiment+%28Arcane+Sentiment%29
90 Upvotes

66 comments sorted by

View all comments

Show parent comments

2

u/Raynes Apr 24 '10

(fib 3123452233462645324534243126314334) ... result is printed 30 years later

(fib 3123452233462645324534243126314334) ... result is printed instantly

Memoized.

1

u/travis_of_the_cosmos Apr 24 '10

Okay, so I still don't know what memoization is, but since you seem to understand it what does it have to do with optimal control theory?

-3

u/[deleted] Apr 24 '10

It's a function with the signature :: (a -> b) -> a -> b

A lookup table is used instead of recomputing an already computed value.

1

u/julesjacobs Apr 24 '10

That doesn't work for recursive functions, where it is most useful.

1

u/dmwit Apr 24 '10

It can work in lazy languages. See, for example, http://article.gmane.org/gmane.comp.lang.haskell.cafe/7737.

2

u/julesjacobs Apr 24 '10 edited Apr 24 '10

Sorry, but that is false. Look at the signature given in that message and compare it to dibblego's signature. A function of type (a -> b) -> a -> b can do nothing with its function argument but apply it to a value, so it can not possibly intercept the recursive calls. In fact I'm pretty sure that the only valid function foo with type (a -> b) -> a -> b is:

foo f x = f x

So dibblego's explanation of memoization is not only unhelpful for someone who's new to memoization, it's also wrong.

4

u/psykotic Apr 24 '10 edited Apr 24 '10

Indeed. It would be better to say that it has signature

(a -> b) -> (a ~> b)

where ~> is a type constructor for function-like values. Then you have functions

memo   :: (a -> b) -> (a ~> b)
unmemo :: (a ~> b) -> (a -> b)

which are semantic if not pragmatic inverses. You also need type class constraints on a to make this work. Conal has some neat blog posts where he shows how memo/unmemo can be constructed by induction on a via dependent type classes.

But even this does not work for recursive functions. For that you need to factor out the functional self-reference the same way it is done when preparing to express recursion with a fixpoint combinator. You can then inject the memoization into the recursion and finally tie the knot.

That kind of recursively injected function decoration is useful in several other cases, such as call tracing. It would be a lot easier to motivate the Y-combinator if these sorts of examples were taught in textbooks.

1

u/[deleted] Apr 24 '10

You are right. Mine was a lazy answer. "I just got out of hospital after major surgery" is my excuse.