cancel
Showing results for
Did you mean:

## not getting the desired behavior

New Contributor
Hi all,

for a sample input: x:-1 2 3 4 -1 6 7 8 9 -1
why does this work -> 0 (min+)\x, and this doesn't (min+\)x?

Thanks.
6 REPLIES 6
New Contributor III
Before others can understand your question, would you please specify what is considered "desired behaviour" in the first place?

@see http://www.catb.org/~esr/faqs/smart-questions.html

New Contributor
sorry! I was in a bit hurry. Thanks for sharing smart questions link.

So, the question is:

For sample input, x::-1 2 3 4 -1 6 7 8 9 -1, I am expecting same output for both the following expressions,
1) 0 (min+)\x
output -> -1 2 3 4 -1 6 7 8 9 -1

2) (min+\)x
output -> -1

Why is the output for 2) not the same as 1)? Where is the thought process going wrong?

Thanks,
New Contributor
the first example uses the 'do' syntax: https://code.kx.com/q/ref/accumulators/#do

so you are running the function (min+) (which is a curiosity to me) zero times.  the result is thus the same as the input.

the second example runs (min+\) which is the same as 'min sums' on your input.  the result is thus -1

i think the question is, what did you expect to happen and why do you think they should produce the same output?

New Contributor III
It's not 'min sums x' but 'min x'
It appears the interpreter doesn't like the composition & 'sums' is not evaluated

q)x:-1 2 3 4 -1 6 7 8 9 -1
q)(min+\)x
-1
q)min(+\)x
-1
q)(max+\)x
9
q)max(+\)x
37
q)((::)+\)x
-1 2 3 4 -1 6 7 8 9 -1
q)
q)parse"(min+\\)x"
((\;+);min)
`x
q)x:20000000#x
q)\ts (+\)x
83 268435648
q)\ts (min+\)x
17 848
q)\ts min x
18 512

New Contributor
as your 'parse' reveals, it is actually 'sums min':

q)0N!parse "sums min";
(+\;min)

and since sums on an atom returns the original atom, the result is 'min':

q)sums min
min

New Contributor
Thanks Nick, Jason for your explanations! I was trying to understand composition of functions, since the left side of the 2 functions should be a monadic/unary, I randomly picked min. Even for - 0 (min+)\ x, I was expecting the result to be same as (+\)x since, I expected it to sum the first two numbers, take min, then use that result to sum with the next number, and so on. I even have question on that.

Why I expected the sample output was:
for a diadic function, f, eg, +, "(f\)x" and "0 f\ x" should be result in same output. What I realized is, (min+\) is not same as (min+)\, since it would be evaluated right to left, so (+\), and then joining it with min would result in different kind of composition, as pointed by Jason in parse result.

PS: Still thinking why its translating to sums min? Is it bug?