How do you make a generic memoize function in Haskell?

后端 未结 5 1138
清酒与你
清酒与你 2020-12-23 22:25

I\'ve seen the other post about this, but is there a clean way of doing this in Haskell?

As a 2nd part, can it also be done without making the function monadic?

5条回答
  •  不知归路
    2020-12-23 22:38

    The package data-memocombinators on hackage provides lots of reusable memoization routines. The basic idea is:

    type Memo a = forall r. (a -> r) -> (a -> r)
    

    I.e. it can memoize any function from a. The module then provides some primitives (like unit :: Memo () and integral :: Memo Int), and combinators for building more complex memo tables (like pair :: Memo a -> Memo b -> Memo (a,b) and list :: Memo a -> Memo [a]).

提交回复
热议问题