I'm running a number of times now into a similar pattern which is error-prone (typos can skip some caching) and simply doesn't look nice to me. Is there a better way of writing something like this?
sum_with_cache' result cache ((p1,p2,p3,p4):partitions) = let (cache_p1, sol1) = count_noncrossing' cache p1 (cache_p2, sol2) = count_noncrossing' cache_p1 p2 (cache_p3, sol3) = count_noncrossing' cache_p2 p3 (cache_p4, sol4) = count_noncrossing' cache_p3 p4 in sum_with_cache' (result+(sol1*sol2*sol3*sol4)) cache_p4 partitions So basically N operations which can update the cache?
I could write also something like:
process_with_cache' res cache _ [] = (cache, res) process_with_cache' res cache f (x:xs) = let (new_cache, r) = f cache x in process_with_cache' (r:res) new_cache f xs process_with_cache = process_with_cache' [] But that doesn't look really clean either. Is there a nicer way of writing this code?
Control.Monad.State.