# not getting the desired behavior

**URL:** <https://forum.kx.com/t/not-getting-the-desired-behavior/12100>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [December 17, 2020, 2:32pm UTC](https://forum.kx.com/t/not-getting-the-desired-behavior/12100 "2020-12-17T14:32:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jainbhavya\_p241](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jainbhavya\_p241](https://forum.kx.com/u/jainbhavya_p241)\
**Post date:** [December 17, 2020, 2:32pm UTC](https://forum.kx.com/t/not-getting-the-desired-behavior/12100/1 "2020-12-17T14:32:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [December 18, 2020, 7:23am UTC](https://forum.kx.com/t/not-getting-the-desired-behavior/12100/2 "2020-12-18T07:23:00Z")

</div>

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](http://www.catb.org/~esr/faqs/smart-questions.html)

---

<div class="post-metadata">

**Author:** ![jainbhavya\_p241](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jainbhavya\_p241](https://forum.kx.com/u/jainbhavya_p241)\
**Post date:** [December 18, 2020, 2:07pm UTC](https://forum.kx.com/t/not-getting-the-desired-behavior/12100/3 "2020-12-18T14:07:00Z")

</div>

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)&nbsp;0 (min+)\x

output -\> -1 2 3 4 -1 6 7 8 9 -1

2)&nbsp;(min+)x

output -\> -1

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

Thanks,

---

<div class="post-metadata">

**Author:** ![Nick10](https://avatars.discourse-cdn.com/v4/letter/n/e68b1a/32.png) [@Nick10](https://forum.kx.com/u/Nick10)\
**Post date:** [December 24, 2020, 6:31pm UTC](https://forum.kx.com/t/not-getting-the-desired-behavior/12100/4 "2020-12-24T18:31:00Z")

</div>

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.&nbsp; the result is thus the same as the input.

the second example runs (min+) which is the same as ‘min sums’ on your input.&nbsp; 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?

---

<div class="post-metadata">

**Author:** ![Jason\_Fealy1](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jason\_Fealy1](https://forum.kx.com/u/Jason_Fealy1)\
**Post date:** [December 24, 2020, 7:55pm UTC](https://forum.kx.com/t/not-getting-the-desired-behavior/12100/5 "2020-12-24T19:55:00Z")

</div>

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

&nbsp;

---

<div class="post-metadata">

**Author:** ![Nick10](https://avatars.discourse-cdn.com/v4/letter/n/e68b1a/32.png) [@Nick10](https://forum.kx.com/u/Nick10)\
**Post date:** [December 24, 2020, 9:02pm UTC](https://forum.kx.com/t/not-getting-the-desired-behavior/12100/6 "2020-12-24T21:02:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jainbhavya\_p241](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jainbhavya\_p241](https://forum.kx.com/u/jainbhavya_p241)\
**Post date:** [December 24, 2020, 9:17pm UTC](https://forum.kx.com/t/not-getting-the-desired-behavior/12100/7 "2020-12-24T21:17:00Z")

</div>

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?
