x:-y

What is the functional form of x-:y . Code is below:

x:11

y:5

x-:y

q)parse “x-:y”
    -:
    x     y

Mohammad Noor

Is :- an operator. How does the value of x actually change ?

q)x:11

q)y:5

q)x-:y

q)x

6

q)y

5

What do you think -: did?

Obviously I can see that x was subtracted and assigned a value of 6. However, the question is how ?

I see this in the parse output:

q)parse “x-:y”

-:

`x

`y

Is -: an operator ?

in http://code.kx.com/wiki/JB:QforMortals/functions

Amend (:)

Amend in C Language

We have already seen the basic form of assignment using amend

a:42

Programmers from languages with C heritage will be familiar with expressions such as

x += 2; // C expression representing amend

which is a shorthand form of

x = x + 2; // C expression

This is usually read simply “add 2 to x” but more precisely is, “assign to x the result of adding 2 to the current value of x.” This motivates the interpretation of such an operation as “amend”, in which x is re-assigned the value obtained by applying the operation + to the operands x and 2. This implies that to amend a variable it must have been previously assigned.

Simple Amend

In q, the equivalent to the above C expression uses +: as the operator.

x:42 x+:2 x44

There is nothing special about + in the above discussion. Amend is available with any binary verb, as long as the operand types are compatible.

a:42 a-:1 a41

We shall see interesting examples of amend with other operators in later chapters.

Thank You. So, :- is an operator. parse “x-:y” shows the same thing.

However, the next question is why did they not document this on the http://code.kx.com/wiki/Reference page. 

Even when you see reference for : and - ; you do not see a mention of that.

Thanks.

Yes, they do,

If you look carefully in the reference page http://code.kx.com/wiki/Reference/Colon

The third line is a link to http://code.kx.com/wiki/JB:QforMortals2/functions#Amend_.28:.29

with the same information that I posted earlier… 

Reference/Colon

[return](“http://code.kx.com/wiki/JB:QforMortals2/execution_control#Return_and_Signal” "“JB:QforMortals2/execution”)

[amend](“http://code.kx.com/wiki/JB:QforMortals2/functions#Amend_.28:.29” ““JB:QforMortals2/functions””)

One more obscure use of the colon is to explicitly select the single-argument meaning of a function. This is not particularly readable but can save characters.

q),:[3] / same as enlist 3,3

Cheers

El martes, 7 de julio de 2015, 14:06:02 (UTC+2), analyst escribió:

What is the functional form of x-:y . Code is below:

x:11

y:5

x-:y

It’s not a true formal operator as it cannot be used with an adverb, just as you cannot do an (assign each) :’ as far as I know in q.