# Functions and adverbs

**URL:** https://forum.kx.com/t/functions-and-adverbs/11312
**Category:** Community Support
**Tags:** kdb-and-q
**Created:** [October 21, 2016, 2:59pm UTC](https://forum.kx.com/t/functions-and-adverbs/11312 "2016-10-21T14:59:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![VA1](https://avatars.discourse-cdn.com/v4/letter/v/7bcc69/32.png) [@VA1](https://forum.kx.com/u/VA1)
#### Post date: [October 21, 2016, 2:59pm UTC](https://forum.kx.com/t/functions-and-adverbs/11312/1 "2016-10-21T14:59:00Z")

</div>

Good day eveyrbody,&nbsp;

I am currently reading Q for Mortals and I have a problem with understanding how functions in q work with adverbs. Allow me to explain:

1. Define a function:&nbsp;

{[x;y] x+y}

1. Then if I want to apply the function to the arguments:

{[x;y] x+y}[1;2]

3

1. If I want to apply the function to a list and sum it “over” using adverb “/”

0 {[x;y] x+y}/ 1 2 3 4 5

15

The confusing part follows:

1. Why does this work?&nbsp;

{[x;y] x+y}/[1;2]

3

1. And this does not.&nbsp;

0 {[x;y] x+y}/[1;2]

1. Why does this work?

0 {[x;y] x+y}/1 2 3

6

1. And this does not.

0 {[x;y] x+y}/[1 2 3]

'type

1. This does not work as well AND it produces different error message…

0 {[x;y] x+y}/ [1 2 3 4 5]

'length

I would absolutely love to know why equation (7) and equation (8) produce different error messages.&nbsp;

Regards

VA.&nbsp;

---

<div class="post-metadata">

### Author: ![zhuo\_dev](https://avatars.discourse-cdn.com/v4/letter/z/22d042/32.png) [@zhuo\_dev](https://forum.kx.com/u/zhuo_dev)
#### Post date: [October 22, 2016, 9:43am UTC](https://forum.kx.com/t/functions-and-adverbs/11312/2 "2016-10-22T09:43:00Z")

</div>

Well, I’m also a newbie and didn’t read the book yet, though I’ll try to explain it:

{[x;y] x+y}&nbsp; // the function definition

{[x;y] x+y}[1;2] // call the function, in the normal form, func[arg1; arg2; …]

0 {[x;y] x+y}/ 1 2 3 4 5 // call the derived function modified by over as a verb, in (and it must be in) the juxtaposition form, without “”, e.g 1 + 2 3 4, not 1 + [2 3 4]

0 {[x;y] x+y}/[1;2]

0 {[x;y] x+y}/[1 2 3]

0 {[x;y] x+y}/ [1 2 3 4 5]  
// as mentioned above, you’re trying call it as a verb with juxtaposition, but using a “”, so it is explained in a different way, see below

the last 3 lines are explained as {0 x}[{[x;y] x+y}/ […] ], which is  
&nbsp;{0 x}[3]

&nbsp;{0 x}[6]

&nbsp;{0 x}[15]

that means send 3,6,15 to the current console (0 is treated as a handler, see http://code.kx.com/wiki/Reference/Number)

But I don’t know why the error of {0 x}[3] and {0 x}[15] are different, sorry.

–  
This email address (zhuo.dev\<at\>gmail.com) is only for development affairs, e.g. mail list, please mail to zhuo\<at\>hexoasis.com or zhuoql\<at\>zoho.com for other purpose.

ZHUO QL (KDr2), http://kdr2.com

---

<div class="post-metadata">

### Author: ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)
#### Post date: [October 23, 2016, 1:32am UTC](https://forum.kx.com/t/functions-and-adverbs/11312/3 "2016-10-23T01:32:00Z")

</div>

I hadn’t intended for this to be so wordy - apologies.

Parsing…

the core grammar is: E:E;e|e e:nve|te| t:n|v v:tA|V n:t[E]|(E)|{E}|N

the relevant parts to adverb and application are:

v:tA (a verb is a term followed by an adverb.

n:t[E] (a term followed by a ‘[’)

&nbsp; &nbsp;

1. Why does this work?  
{[x;y] x+y}/[1;2]  
3

this parses as t[E] where t is a verb (tA) where the lambda is a term (t)

q)0N!parse"{[x;y] x+y}/[1;2]";

((/;{[x;y] x+y});1;2)

t is a verb that can accept one or two arguments. two arguments are provided as E (1 and 2) and is equivalent to the juxtapose

q)0N!parse"1{[x;y] x+y}/2";

((/;{[x;y] x+y});1;2)

  

note that the first arg in the call is the "x" argument (left) and the second is "y"  

1. And this does not.&nbsp;

0{[x;y] x+y}/[1;2]

this expression is equivalent to:  
 0 ( 1{[x;y] x+y}/2 )

q)0N!parse"0{[x;y] x+y}/[1;2]";

(0;((/;{[x;y] x+y});1;2))

that is, 0(6) - and the function “zero” does not like integers: http://code.kx.com/wiki/Reference/Zero

it can be thought that kdb+ evaluates from right to left of “right of left”, but it is actually the parsing rules that cause the right of left “evaluation”.

considering e:nve|te|e| and “1-2+3”. using nve: (1-“2+3”) -\> (1-(2+“3”)) -\> (1-(2+(3))) (last step by e:te|)

the actual tree is:

q)0N!parse"1-2+3";

(-;1;(+;2;3)) /verb followed by arguments - recursively evaluated

1. And this does not.  
0 {[x;y] x+y}/[1 2 3]  
'type

we know that {[x;y] x+y}/[1 2 3] evaluates to 6. so the expression reduces to 0(6).

as soon as you use the n:t[E] syntax, the t expression becomes a noun and there is no way to pass an x/left argument to a noun by using juxtaposition (only verbs allow arguments to their left - oh, and an adverb takes a single term to it’s left)

1. This does not work as well AND it produces different error message…  
0 {[x;y] x+y}/ [1 2 3 4 5]  
'length

this one seems to be a curiosity - 0 must have at least two constraints (type and length) and the length constraint isn’t violated until an integer argument exceeds 9. the length constraint is checked first and so when length constraint is violated… that’s my guess.

---

<div class="post-metadata">

### Author: ![sieber](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@sieber](https://forum.kx.com/u/sieber)
#### Post date: [October 25, 2016, 10:45am UTC](https://forum.kx.com/t/functions-and-adverbs/11312/4 "2016-10-25T10:45:00Z")

</div>

> The confusing part follows:
> 
> 1. Why does this work?&nbsp;
> 
> {[x;y] x+y}/[1;2]
> 
> 3

{[x;y]x+y} &nbsp;is equivalent to &nbsp;{x+y},

{x+y}/ &nbsp; &nbsp;produces a “new” function which has &nbsp;one OR two arguments.

q allows you to call a funciton with &nbsp;two arguments via &nbsp; function[param1;param2] &nbsp; OR &nbsp;in some cases (for example + or - and THIS NEW function) &nbsp;via &nbsp;param1 function param2 &nbsp; (=\> &nbsp;example 1+3)

If used with two arguments the first one is just the first step in the “over” (/) reduce.

so {x+y}/[1;2] &nbsp; is &nbsp;the same as &nbsp; 1{x+y}/enlist 2 &nbsp;which is the same as &nbsp; &nbsp;{x+y}/[1 2]…

&nbsp;

> 1. And this does not.&nbsp;
> 
> 0 {[x;y] x+y}/[1;2]

now you try to call the new function with 3 parameters: first 0, second 1 third 2. &nbsp;As the “new” function has only max 2 arguments, you get an error

&nbsp;

> 1. Why does this work?
> 
> 0 {[x;y] x+y}/1 2 3
> 
> 6

you call it again with two arguments: first 0 &nbsp; &nbsp;second list of 1 2 and 3

&nbsp;

> 1. And this does not.
> 
> 0 {[x;y] x+y}/[1 2 3]
> 
> 'type

here you call &nbsp;the new function with one parameter: 1 2 3. &nbsp;This results 6. &nbsp;Now &nbsp;you apply &nbsp;6 to 0. &nbsp; (same as 0@6 &nbsp;or 0[6], ..) which gives a type error

&nbsp;

> 1. This does not work as well AND it produces different error message…
> 
> 0 {[x;y] x+y}/ [1 2 3 4 5]
> 
> 'length
> 
> I would absolutely love to know why equation (7) and equation (8) produce different error messages.&nbsp;

cant tell you why you get a different error. &nbsp;But you call &nbsp; 0@15 here which produces a length error &nbsp;0@6 produces type error. no idea why..

I think you &nbsp;should read the &nbsp;function part in the tutorial:&nbsp;[http://code.kx.com/wiki/JB:QforMortals2/primitive\_operations#Introduction\_to\_Functions](http://code.kx.com/wiki/JB:QforMortals2/primitive%5C_operations#Introduction%5C_to%5C_Functions)

In q/k you have many variants on how you can execute a function. It is fundamentally important to understand this

regards, Markus

---

<div class="post-metadata">

### Author: ![VA1](https://avatars.discourse-cdn.com/v4/letter/v/7bcc69/32.png) [@VA1](https://forum.kx.com/u/VA1)
#### Post date: [October 26, 2016, 12:37pm UTC](https://forum.kx.com/t/functions-and-adverbs/11312/5 "2016-10-26T12:37:00Z")

</div>

Very detailed answers, thank you very much I shall go through the explanations today.&nbsp;
