Not sure, whether this went through to the mailing list or only to Mickael, so please, forgive me, if posting twice.

On Mon, Mar 23, 2020 at 11:05 AM Mickael Desfrenes <mickael.desfrenes@unicaen.fr> wrote:
Is there any known solution for function memoization in basex ? Or maybe a cache implementation ?

Not sure how much this helps, but there was a Balisage talk by James Fuller, titled "A catalog of Functional Programming idioms in XQuery 3.1", which produces (amongst others) a memoize function. It is written in the Marklogic dialect of XQuery. but porting it was a no-brainer, since, AFAIR, only the syntax for maps had to be changed. The Github repo is here.  
 
--
Minden jót, all the best, Alles Gute,
Andreas Mixich