Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Memoization
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Memoization without recursion == You can just write a memoization function using a data structure that is suitable for your application. We don't go into the details of this case. If you want a general solution for several types, you need a type class, say <code>Memoizable</code>. <haskell> memoize :: Memoizable a => (a->b) -> (a->b) </haskell> Now, how to implement something like this? Of course, one needs a finite map that stores values <code>b</code> for keys of type <code>a</code>. It turns out that such a map can be constructed recursively based on the structure of <code>a</code>: <haskell> Map () b := b Map (Either a a') b := (Map a b, Map a' b) Map (a,a') b := Map a (Map a' b) </haskell> Here, <code>Map a b</code> is the type of a finite map from keys <code>a</code> to values <code>b</code>. Its construction is based on the following laws for functions <haskell> () -> b =~= b (a + a') -> b =~= (a -> b) Γ (a' -> b) -- = case analysis (a Γ a') -> b =~= a -> (a' -> b) -- = currying </haskell> For further and detailed explanations, see * Ralf Hinze: [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.43.3272 Memo functions, polytypically!] * Ralf Hinze: [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.8.4069 Generalizing generalized tries] * Conal Elliott: [http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/ Elegant memoization with functional memo tries] and other [http://conal.net/blog/tag/memoization/ posts on memoization]. * Conal Elliott [http://conal.net/papers/type-class-morphisms/ Denotational design with type class morphisms], section 9 (Memo tries).
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width