cancel
Showing results for 
Search instead for 
Did you mean: 

Function of syntax `

YPangalela
New Contributor

Hi, what is the function of the symbol ` ? Sorry I don't know how to call it

Sometimes I find it together with $ . For example:

a: "1"
show b: ` $a

Thanks

2 ACCEPTED SOLUTIONS

Michaela
Community Manager Community Manager
Community Manager

Hi Ypangalela,

Backtick represents a symbol. In your example the function of $ is to cast (or transform) a string to a symbol - see more examples here https://code.kx.com/q4m3/7_Transforming_Data/#732-creating-symbols-from-strings 
and also here  https://kx.com/learning/modules/casting/#casting-methods 

 

Hope this is helpful/

Thanks,

Michaela

 

 

View solution in original post

example #1

  • b defined globally as 6
  • b defined locally as 42 using colon : 
  • b defined globally as x (98) using set 
  • local b returned as 42
  • global b returned as 98

example #2

  • b defined globally as 6
  • b defined locally as 42 using colon : 
  • nothing is happening as set without backtick does not do assignment
  • local b returned as 42
  • global b returned as 6

 

So the reason for this behavior has more to do with function scoping than backtick. You can learn more about function scoping (defining locally verses globally) here, specifically around 4.30mins you can see in action the two ways of defining variables globally - using set and double colon.

Hope this helps!

 

View solution in original post

4 REPLIES 4

Michaela
Community Manager Community Manager
Community Manager

Hi Ypangalela,

Backtick represents a symbol. In your example the function of $ is to cast (or transform) a string to a symbol - see more examples here https://code.kx.com/q4m3/7_Transforming_Data/#732-creating-symbols-from-strings 
and also here  https://kx.com/learning/modules/casting/#casting-methods 

 

Hope this is helpful/

Thanks,

Michaela

 

 

PCarroll
Community Manager Community Manager
Community Manager

Also, this just brings up good questions about symbols versus strings & when and why we use them. 
Many languages including q intern strings so that only one copy of any distinct value is stored. Any other references to the same string are stored as just that, references.
This helps with memory management, and improves performance on equality comparison i.e. 

// assuming col1 is symbol column
q)select from tab where col1 in `a`b

Storing the same string e.g. "New York Stock Exchange" repeatedly in memory would require a greater amount of memory than its symbol counterpart.

Because q typically stores the string pool entirely in memory (that's what your sym file is in a typical kdb+ database) consideration should be given as to what columns are symbol and which are string. 

ℹ️ If the string list contains many short repeated values this may be a good candidate for casting to a symbol

Hi Michaela, thanks for the answer

Now I'm studying about function and encountering the same problem about backtick.

Let me give 2 examples.

 

Example #1:

b:6
f:{b:42; `b set x; b}

f[98]
b

 

Example #2:

b:6
f:{b:42; b set x; b}

f[98]
b

 

In example #1, the value of b becomes 98, but in example #2, it stays 6

In this case, what's the function of backtick? 

Thanks

example #1

  • b defined globally as 6
  • b defined locally as 42 using colon : 
  • b defined globally as x (98) using set 
  • local b returned as 42
  • global b returned as 98

example #2

  • b defined globally as 6
  • b defined locally as 42 using colon : 
  • nothing is happening as set without backtick does not do assignment
  • local b returned as 42
  • global b returned as 6

 

So the reason for this behavior has more to do with function scoping than backtick. You can learn more about function scoping (defining locally verses globally) here, specifically around 4.30mins you can see in action the two ways of defining variables globally - using set and double colon.

Hope this helps!