Consider the following Python program:
a = lambda x: x
b = lambda x: x
print(a == b)
This obviously outputs False although, which is clear why it happens. However, this is counterintuitive. I wonder if there is any programming language (not academic-only) that is able to do a structural comparison of lambda expressions and would print True in the example above? If this doesn't work out of the box, is there any smart way to compare lambda expressions at all? How about the .NET abstract syntax trees?
Edit: As I got the answer, here is working example:
# Applies an argument arg to a function f.
apply = lambda f, arg: lambda *args, **kwargs: f(arg, *args, **kwargs)
# Compare two functions by their co_code (see answer below)
equals = lambda l0, l1: l0.__code__.co_code == l1.__code__.co_code
# Defines a function that adds 2 to a provided number.
add_two_0 = apply(add, 2)
# Another way of adding two is twice adding 1.
add_two_1 = apply(apply(add, 1), 1)
# The following statement prints True
equals(add_two_0, add_two_1)