Functional Programming: Creating Lambda Functions in Haskell and Python
Functional programming is a paradigm, which means that it doesn’t have an implementation. The basis of functional programming is lambda calculus, which is actually a math abstraction. Consequently, when you want to perform tasks by using the functional programming paradigm, you’re really looking for a programming language that implements functional programming in a manner that meets your needs. Two languages that are ideal for functional programming are Haskell and Python.
Creating lambda functions in Haskell
You can create functions in Haskell. For example, if you want to create a curried function to add two numbers together, you might use
add x y = x + y. This form of code creates a definite function. However, you can also create anonymous functions in Haskell that rely on lambda calculus to perform a task. The difference is that the function actually is anonymous — has no name — and you assign it to a variable. To see how this process works, open a copy of the Haskell interpreter and type the following code:
add = \x -> \y -> x + y
Notice how lambda functions rely on the backslash for each variable declaration and the map (->) symbol to show how the variables are mapped to an expression. You now have a lambda function to use in Haskell. To test it, type add 1 2 and press Enter. The output is
3 as expected.
Obviously, this use of lambda functions isn’t all that impressive. You could use the function form without problem. However, lambda functions do come in handy for other uses. For example, you can create specially defined operators. The following code creates a new operator, +=:
(+=) = \x -> \y -> x + y
To test this code, you type 1+=2 and press Enter. Again, the output is
3, as you might expect. Haskell does allow a shortcut method for defining lambda functions. You can create this same operator using the following code:
(+=) = \x y -> x + y
Creating lambda functions in Python
As with the Haskell function, you can also create a lambda function version of the
add function. When creating a lambda function in Python, you define the function anonymously and rely on the
lambda keyword, as shown here:
add = lambda x, y: x + y
Notice that this particular example assigns the function to a variable. However, you can use a lambda function anywhere that Python expects to see an expression or a function reference. You use this function much as you would any other function. Type add(1, 2), execute the code, and you see
3 as output.
If you want to follow a more precise lambda function formulation, you can create the function like this:
add = lambda x: lambda y: x + y
In this case, you see how the lambda sequence should work more clearly, but it’s extra work. To use this function, you type add(1)(2) and execute the code. Python applies the values as you might think, and the code outputs a value of
3. Python doesn’t allow you to create new operators, but you can override existing operators; this article tells you how. However, here you create a new use for the letter X using a lambda function. To begin this process, you must install the Infix module by opening the Anaconda Prompt, typing pip install infix at the command prompt, and pressing Enter. After a few moments, pip will tell you that it has installed Infix for you. The following code will let you use the letter X to multiply two values:
from infix import mul_infix as Infix X = Infix(lambda x, y: x * y) 5 *X* 6 X(5, 6)
The first statement imports
Infix. You have access to a number of infix methods, but this example uses this particular one. The website pypiu.org discusses the other forms of infix at your disposal.
The second statement sets
X as the infix function using a lambda expression. The manner in which Infix works allows you to use
X as either an operator, as shown by
5 *X* 6 or a regular function, as shown by
X(5, 6). When used as an operator, you must surround
X with the multiplication operator,
*. If you were to use
shif_infix instead, you would use the shift operators (
>>) around the lambda function that you define as the operator.