0
$\begingroup$

I wrote the following recursion, assuming that the function would gradually decrease the size of list k by 1 (via Drop). Reap and Sow indicate that a different output is produced. I do not immediately see where the issue is. Clearly the increasing number of brackets in the output indicates that the structure is affected rather than the list values, which should gradually decrease by 1 in number due to Drop.

 f = K[K[K[K[a,a],a],a],a]

 check2[f_,k_]:= 1 /; AtomQ[f]
 check2[K[g_, h_], k_] :=
 Reap[(Sow[Drop[k, 1]]; Sow[check2[g, k]])]

 In[140]:= check2[f, {1, 1, 1, 2}]

 Out[140]= {{{{1, {{{1, 1, 2}, 1}}}, {{{1, 1, 
  2}, {1, {{{1, 1, 2}, 1}}}}}}, {{{1, 1, 
 2}, {{1, {{{1, 1, 2}, 1}}}, {{{1, 1, 
    2}, {1, {{{1, 1, 2}, 1}}}}}}}}}, {{{1, 1, 
2}, {{{1, {{{1, 1, 2}, 1}}}, {{{1, 1, 
    2}, {1, {{{1, 1, 2}, 1}}}}}}, {{{1, 1, 
   2}, {{1, {{{1, 1, 2}, 1}}}, {{{1, 1, 
      2}, {1, {{{1, 1, 2}, 1}}}}}}}}}}}}
$\endgroup$
1
$\begingroup$

Frankly, I'm not quite sure what you exactly want to do. I guess the following code is what you want:

f = K[K[K[K[a, a], a], a], a];

check2[f_, k_] := 1 /; AtomQ[f]
check2[K[g_, h_], k_] := (Sow[k]; check2[g, Drop[k, 1]]);

Reap[check2[f, {1, 1, 1, 2}]]
$\endgroup$

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.