haskell - Functional alternative to caching known "answers" -
i think best way form question example...so, actual reason decided ask because of because of problem 55 on project euler. in problem, asks find number of lychrel numbers below 10,000. in imperative language, list of numbers leading final palindrome, , push numbers list outside of function. check each incoming number see if part of list, , if so, stop test , conclude number not lychrel number. same thing non-lychrel numbers , preceding numbers.
i've done before , has worked out nicely. however, seems big hassle implement in haskell without adding bunch of arguments functions hold predecessors, , absolute parent function hold of numbers need store.
i'm wondering if there kind of tool i'm missing here, or if there standards way this? i've read haskell kind of "naturally caches" (for example, if wanted define odd numbers odds = filter odd [1..], refer whenever wanted to, seems complicated when need dynamically add elements list.
any suggestions on how tackle this?
thanks.
ps: i'm not asking answer project euler problem, want know haskell bit better!
i believe you're looking memoizing. there number of ways this. 1 simple way memotrie package. alternatively if know input domain bounded set of numbers (e.g. [0,10000)) can create array values results of computation, , can index array input. array approach won't work though because, though input numbers below 10,000, subsequent iterations can trivially grow larger 10,000.
that said, when solved problem 55 in haskell, didn't bother doing memoization whatsoever. turned out fast enough run (up to) 50 iterations on input numbers. in fact, running right takes 0.2s complete on machine.
Comments
Post a Comment