简体   繁体   中英

Memoize functions with `Ord` arguments in Haskell

Is there a common Haskell idiom for memoizing recursive functions with type

Ord a => a -> SomeType

In particular, I have a recursive function with type

(Int, Int, [Int]) -> Int

that I want to memoize.

Your data type

(Int, Int, [Int])

can be mapped onto Integer . The first 2 Int s are just a bunch of bits, and the list of Int s is just a list of bits, coming in bunches at a time. Since it's possible to make a memoization tree for an Integer , it's possible to make a memoization tree for this data type. The memo-trie package agrees, providing the following 3 relevant instances:

instance HasTrie Int
instance HasTrie x => HasTrie [x]
instance (HasTrie a, HasTrie b, HasTrie c) => HasTrie (a, b, c)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM