Being rather new to pure functional programming idiom, I can't get how to implement this case of dynamic programming. I have a function f :: String -> [String] which is calculated recursively and want to memoize it. Input Strings can be arbitrary, so I guess that something like a lazy Map is needed, but couldn't find any. How to implement such case in Haskell?
-
3Have you looked on Hackage? There's about a dozen memoization libraries available...Daniel Wagner– Daniel Wagner2012-10-14 09:32:39 +00:00Commented Oct 14, 2012 at 9:32
Add a comment
|
1 Answer
Use a memoizer library:
import qualified Data.MemoCombinators as Memo
f :: String -> [String]
f = Memo.list Memo.char memof -- because String = [Char]
where
memof x = ... f ... -- call *f* recusively (not memof)
See the documentation for more. Also see MemoTrie
1 Comment
aplavin
I didn't think that it's implemented as an external library and didn't search search at hackage. And it seems strange that such functions are not in the default GHC libraries.