Identities in various languages

Objective-C. Unfortunately Objective-C doesn’t support type interface. So let’s use int for now.

^(int x) { return x; }

C++. You need two characters, not one, for expressing lambda expression.

[](auto x) { return x; }

Swift. Remember AWK. Identity is identical.

{ $0 }

Ruby. Think that -> is a lambda. If you are using v1.8 or earlier, use lambda { |x| x }.

-> x { x }

ECMAScript. If you are using v5 or earlier, use function (x) { return x; }.

x => x

Python. You can make an identity in very sophisticated way.

lamda x: x

Scheme. Like Python, you must type lambda. Though you can rename it to fn or anything you like, the bulit-in keyword is lambda, and people love it.

(lambda (x) x)

Haskell. You can type \ instead of lambda.

\x -> x

Unlambda. Unlambda has a built-in identity i.


C. Actually C doesn’t have any identity operator, and you cannot make your own. However, the unary plus operator is an identity of numerical types like int or float.