Last Updated: February 25, 2016
· dionysios

Function Match in Q

The binary match operator (~) can be applied to functions, but the result of the operation might be counterintuitive.

Two functions only match if and only if their definitions are identical.

For example:

f: {x * x}
f1: {[x]; x * x}
f2: {:x * x}
f3: {[a]; a * a}
f4: { x    *    x}

f ~ f1  / Result: 0b
f ~ f2  / Result: 0b
f ~ f3  / Result: 0b
f ~ f4  / Result: 0b

In other words, even if the I/O mappings of two functions are the same, the match function may not return 1b.
So essentially, the result of the match operator is the result of matching the underlying strings:

f ~ g

is equivalent to

(string f) ~ (string g)

On a different note, the match operation is a nice way to check if two built-in expressions are equivalent:

(each[reverse]) ~ (reverse each)  /Result: 1b