2022.07.29 12:00 PM - edited 2022.07.29 12:27 PM
I have below code, which doesn't work. h and g are 2 projections defined inside f, and they are referencing to each other.
Theorectically, the answer should be:
f[10]=g[10]
=h[8] + 20
=g[5] + 30 + 20
=h[3] + 20 + 30 + 20
=g[0] + 30 + 20 + 30 + 20
=h[-2] +20 + 30 + 20 + 30 + 20
= -2 + 120 = 118
f:{[xF]
g:{[h;xG] $[xG < 0; xG; h[xG - 2] + 20]}[h]; // doesn't work, as h is not yet defined
h:{[g;xH] $[xH < 0; xH; g[xH - 3] + 30]}[g];
g[xF]
};
f[10];
How to make it works?
2022.07.29 12:18 PM
f:{[xF]
g:{[h;xG] $[xG < 0; xG; h[.z.s; xG - 2] + 20]};
h:{[g;xH] $[xH < 0; xH; g[.z.s; xH - 3] + 30]};
g[h; xF]
};
f[10];
Another option is to use .z.s to refer to the calling function when calling the other function.
2022.07.29 12:10 PM - edited 2022.07.29 12:12 PM
This is a chicken and the egg situation. Define g and h without the projections first then define them with the projections.
f:{[xF]
g:{[h;xG] $[xG < 0; xG; h[xG - 2] + 20]};
h:{[g;xH] $[xH < 0; xH; g[xH - 3] + 30]};
g:g[h];h:h[g];
g[xF]
};
2022.07.29 12:21 PM
Thanks for your reply. But the above code doesn't work. I got an "type: Mismatched types" error.
2022.07.29 12:28 PM
With f[10]? Can't spot why and I didn't run the above as on mobile. Not sure why it doesn't work
2022.07.29 12:29 PM
yes, f[10] doesn't work.
2022.07.29 12:31 PM
No worries. Kshephard's solution is nicer anyway. Will take a look later when at a pc to see why.
2022.07.29 12:18 PM
f:{[xF]
g:{[h;xG] $[xG < 0; xG; h[.z.s; xG - 2] + 20]};
h:{[g;xH] $[xH < 0; xH; g[.z.s; xH - 3] + 30]};
g[h; xF]
};
f[10];
Another option is to use .z.s to refer to the calling function when calling the other function.
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.