KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums KX Academy Function of syntax `

  • Function of syntax `

    Posted by ypangalela on January 21, 2022 at 12:00 am

    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

    ypangalela replied 3 months, 2 weeks ago 3 Members · 4 Replies
  • 4 Replies
  • Laura

    Administrator
    January 21, 2022 at 12:00 am

    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

    Member
    January 21, 2022 at 12:00 am

    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

  • ypangalela

    Member
    January 23, 2022 at 12:00 am

    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

  • Laura

    Administrator
    January 24, 2022 at 12:00 am

    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!

     

Log in to reply.