I say odd like a mathematical function, a mathematical function is odd if
f(-x) = -f(x)
I understand that comparing two strings may seem to have little to do with this, because the arguments cannot be negative, but I mean that the functions value should be the negative of it’s value with swapped arguments, i.e.,
distance(str2, str1) = -distance(str1, str2);
5
You could do this with any string distance function.
For example, consider the following distance function:
distance(a, b)
{
return calc(a, b);
}
If you make the order of the arguments significant, then you can invert the sign of the result as needed:
distance(a, b)
{
var first = getFirstAlphabetically({a, b});
var isNegative = (a != first);
var sign = isNegative ? -1 : 1;
return calc(a, b) * sign;
}
Keep in mind this is just pseudocode.
1
Yes, there is a lot of them! This is just the theory of even and odd part of functions adapted to this context.
Take any function h defined on pair of strings and set ĥ(a,b) = h(b,a). Then the odd and even part of h are respectively h- = (h – ĥ)/2 and h+ = (h + ĥ)/2.
Now, if you take any function h defined on pair of strings, then its odd part h- suits your requirements. Furthermore you obtain every interesting function this way, because if h is odd then it is its own odd part h = h-.
It might also be fun to remark, as @MetaFight did, that given any odd function u which only takes the values 1, 0 and -1, then the multiplication by u turns an odd function into an an even function and conversely!