Tried to solve this problem using Clojure’s memoize function but not managed to do it yet.