I'm going to implement a function which takes in an Integer and outputs a lazy, infinite list of coprime Integers.
coprime 2 = [1,3..]
coprime 3 = [1,2,4,5,7,8,....]
I expect that these lists will be accessed multiple times, so I want their values stored as CAFs. (Forgive me if I'm using that term incorrectly) Okay, so that's fine because that will be done automatically by the compiler (I think). But what about distinct inputs which are guaranteed to have the same output? For instance:
coprime 2 = [1,3..]
coprime 4 = [1,3..]
How can I make sure that these two patterns call upon the same CAF so that the result doesn't have to be recomputed?
My first thought is to try implementing coprime so that coprime 2 and coprime 4 actually call a local function with the same argument. A minimal example:
coprime n
  | n == 4    = realcoprime 2
  | otherwise = realcoprime n
    where
      realcoprime n = .....
But do all calls to coprime share the same CAF values associated with realcoprime? And if so, won't they be garbage collected prematurely because they're not in the global scope?
 
     
    