2
$\begingroup$

I have a function that takes in sequences of values (as a list), and recursively removes the largest element from that sequence as part of its computation. Some of these sequences can be quite large, but I suspect that they often have common subsequences. So I want to memoize my function, but I don't want to memoize it for very large sequences, only smaller sequences which these larger sequences might have in common. I tried something like this,

f[sequence_?Length[#] < 16 &] := f[sequence] = f[sequence]

where f is already defined elsewhere. Probably I would need to memoize for larger sequences, but that's the idea of it. However, I hit the recursion limit when I call this function.

$\endgroup$
  • 1
    $\begingroup$ Is the second f[sequence] on the right-hand side actually some expression involving sequence? And as far as the left-hand side goes, I think you need to do f[sequence_?(Length[#] < 16 &)] because of operator precedence issues. $\endgroup$ – march Aug 2 '16 at 23:11
  • $\begingroup$ The second f[sequence] on the right hand side is defined like f[sequence_] := .... Adding parenthesis doesn't change the error I get. $\endgroup$ – Jordy Dickinson Aug 2 '16 at 23:12
  • 2
    $\begingroup$ This feels like an xy problem - why not show an example of what it is you're trying to get to, and why - there's probably a succinct and efficient way to do it... $\endgroup$ – ciao Aug 2 '16 at 23:16
  • $\begingroup$ By the way, it is a good idea to wait for at least a day before accepting an answer. That way, you will likely attract more answers, and they might be more to your liking (that green check-mark can be a deterrent to new answers!). In addition, @ciao's point is well-taken: you might consider outlining your actual problem (although I like the particular question of sometimes-memoization, although now that I think about it, I suspect this might be a duplicate...) $\endgroup$ – march Aug 2 '16 at 23:29
  • $\begingroup$ @march, Thanks, I'll keep that in mind. Also with regards to being a duplicate, I searched but couldn't find anything on this. $\endgroup$ – Jordy Dickinson Aug 2 '16 at 23:43
3
$\begingroup$

Define the function twice, once without the memoization, and once with the memoization and your condition.

Consider this simple example:

Clear@f
f[{}] = {};
f[a_List] := Rest@a
f[a_List /; Length@a <= 3] := f[a] = Rest@a

If we run

FixedPointList[f, Range[5]]
(* {{1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5}, {}, {}} *)

then

enter image description here

Notice that it has saved only those lists of length less than 4.


Alternatively, if it is annoying to define the function twice, do this instead. It works the same.

Clear@f
f[{}] = {};
f[a_List] := If[Length@a > 3, #, f[a] = #]&@Rest@a
$\endgroup$
  • $\begingroup$ This does indeed fix my problem. I just wish there was an elegant way to do it, without all the code repetition. $\endgroup$ – Jordy Dickinson Aug 2 '16 at 23:19
  • 1
    $\begingroup$ @JordyDickinson. In my opinion, this sort of construction is elegant, and is very Mathematica-idiomatic. But I have posted an alternative solution. $\endgroup$ – march Aug 2 '16 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.