Comparisment

   - compare programming languages by example

49

Function literals/ anonymous functions

The syntax for function literals/ anonymous functions (functions without names).

Created by sam

Add a new answer

Either add an answer for a new language or add an alternative solution.
A description is only necessary when the code is not suitably 'obvious' or to justify an alternative approach.

Haskell

add :: Int -> Int -> Int
add = \x -> \y -> x + u

Anonymous functions are created with a '\', an argument, an '-> followed by the result.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Haskell

add :: Int -> Int -> Int
add = \x y -> x + y

Multiple arguments may be accepted by an anonymous function.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.

Python

add = lambda x, y: x + y

Anonymous functions are created with 'lambda' followed by arguments, a colon ':' and the result expression.

Created by: sam

Edit this answer

Edit the language, code and description simultaneously.