cancel
Showing results for
Did you mean:

## infinite loop

New Contributor
I was playing around and got the interpreter to go into an infiniteloop unexpectedly. It's not a major issue though. I think the overadverb (/) gets confused trying to use a monadic lambda as a dyadicfunction.KDB+ 2.7 2010.11.30 Copyright (C) 1993-2010 Kx Systemsm32/ 2()core 4096MB andy slakt.local 10.211.55.2 PLAY 2011.02.28q)p:parse "1 2+/3 4"q)f:p[0;0][{x+1}]q)f 1 2*** it's hung***^C{x+1}'stopq))\q)
5 REPLIES 5
New Contributor
effectively you are doing
{1+x}/[1 2]

which of course will only terminate when it gets to nulls

note that when you break you can investigate
q){1+x}/[1 2]�
{1+x}
'stop
q))x
6009038 6009039
q))\

Regards,
� Attila

New Contributor
On Jan 17, 4:13�pm, Attila Vrabecz wrote:> which of course will only terminate when it gets to nullsWow!Well, my first mistake was not reading further than the discussionof / in Q For Mortals 2, which suggested that / is defined only fordyadic functions. I assumed (without verifying) that q would choke ona unary like {x+1}. Not so.The reference wiki does have definitions for / applied to unaryfunctions (apparently any arity is allowed). However, the describedusage pattern for unary functions requires two arguments to themodified function: an iteration count and the initial state.The example we're talking about, {1+x}/[1 2] supplies only oneargument to {1+x}/ and that argument appears to be the initial state.A less confusing example of the same call pattern would be {1+x}/[1].This calling pattern wasn't mentioned on the wiki, but apparently ispart of the implementation. Presumably, this form will loop likebefore but without a pre-defined iteration count.As you suggested, I played around with breaking in while thecomputation seemed "hung" and verified that it was in fact countingupwards. In this form, q isn't it's usual lightning fast self and I'dhave to wait a bit over 6 minutes (on my laptop) for it to overflow.Here's a spoiler:q){1+x}/[2147483600]0NSo, it does terminate at a null. I think that means f/[x] (for a unaryf) is roughly equivalent to:{while[not null x;x:f x];x}Makes sense. But now I wonder about your suggestion that {1+x}/[1 2]will terminate. My "while loop" version wouldn't terminate because thefunction always returns a list, which is never null. Taking you atyour word (as well as being impatient), I try a spoiler with a listargument:q){1+x}/[2147483600 2147483601]0N 0NHey, it does terminate! Maybe the while loop equivalent needs a tweakto look down a list:{while[not all null x;x:f x];x}But then, what about more complicated situations? Let's see whathappens with nested lists:q)null {1+x}/[(2147483600 2147483601;2147483600 2147483601)]11b11bObviously, this terminates too, so the termination predicate needs todetect any nested structure consisting solely of nulls. Probablysomething along the lines of the following:{while[not {all x}/[null x];x:f x];x}The termination predicate here is (somewhat uncomfortably) defined interms of / to recur down the boolean structure returned by null. Acouple of quick tests suggest that "{all x}/[null x]" is gettingclose:q)not {all x}/[null (2147483600 0N;0N 0N)]1bq)not {all x}/[null (0N 0N;0N 0N)]0bThese two uses of / across booleans reveal that the recursion stopsfor boolean values too. That's important since booleans are nevernull.Is the actual definition of what / does for unary functions calledwith only one argument documented anywhere? Am I close?
New Contributor
> Is the actual definition of what / does for unary functions called with only one argument documented anywhere?doesn't seem to be, but afaik it's a version of backslash "iterate",with the same relation as "over" and "scan"--run until a repeat or theseed is seen, then return the next to last result (instead of all butthe last, which is regular "iterate")q){\$[x<10;x+1;1]}\[1]1 2 3 4 5 6 7 8 9 10q){\$[x<10;x+1;1]}/[1]10q){[f;x]while[not((last x)~last -1_(),x)|\$[0
New Contributor
> doesn't seem to be, but afaik it's a version of backslash "iterate",> with the same relation as "over" and "scan"--run until a repeat or the> seed is seen, then return the next to last result (instead of all but> the last, which is regular "iterate")Yep, that makes much more sense than where I was going. It alsoappears to use tolerant compares, as you'd expect:q)\P 20q)t:2 xexp -43q){t+x}/[1]1.0000000000001137q)t:2 xexp -42q){t+x}/[1]******* spins for a while *******^C{t+x}'stopq))x1.0000067073428909q))\q)Thanks for your help, Aaron. I owe you a pint.
New Contributor
> Thanks for your help, Aaron. I owe you a pint.Attila too. 😉