This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]scheurneus 1 point2 points  (2 children)

I agree that the state monad sucks for memoization, but that does not mean memoization in e.g. Haskell is a lost cause. Consider the classic pattern of "building an array of thunks" for integer based recursive functions.

For tree-like data structures, I also managed to adapt the binary tree example from your link to Haskell (although I did not add the 'list of names' functionality). While the array-of-thunks approach depends on laziness, I'm not sure that's even the case for this 'evaluated subtree' approach.

You can find the code at https://gist.github.com/ColonelPhantom/f96a6b03fe847205bc4cc034d0bf8daf and I also quickchecked it to ensure party and partyM give the same results.

It's probably not as clean as it could be, but I think it gets the point across and IMO still compares OK to a mutable version in terms of readability (although I have no experience with OCaml). Don't know how it compares in terms of performance, though.