Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only T&Cs apply

Find out more

Functional Programming in Other Languages

The concepts of functional programming are spreading to mainstream languages. Jeremy explores functional programming in Python.
5
JEREMY: This is the final screen cast of the course. Here we are. What’s happening? I’m going to start a Python interpreter instead of the usual GHCi.
20.4
Shock. Horror. Why am I reverting to Python– Python 3 to be precise. Let me explain. What I want to do in this final screencast is to show you the functional constructs that pop up in mainstream languages. I admit Haskell isn’t a mainstream language yet, and it’s probably true that no functional programming language is in the same league as Python, Java, or C++. However, functional constructs and ideas are being incorporated into imperative and object-oriented languages. So let’s have a little look at Python. Here is a lambda expression. Lambda x goes to x plus 1. This is the increment function. And we can apply this to an argument.
76.4
Here, applying it to 2, and it returns the value 3. Notice the parentheses. I think Python would complain without these brackets. Now, here’s a map function in Python. I’m going to map my same increment function– x goes to x plus 1 over a list of elements 1, 2, 3.
108
The thing I get back is a map object. I can either enumerate this object or convert it back into a list. When I convert it into a list, I get back the list 2, 3, 4. Each element is one more than the corresponding element in the input list.
126.7
Here’s another map function.
135.1
Hello plus str x– that converts the input values to strings. I’m going to map over– well, let’s just do 1, 2, 3 for now actually. And if I convert this thing back to a list, I can see what’s going on. Hello 1. Hello 2. Hello 3. So the anonymous function, the lambda function, has turned each input element to a string and prepended hello to it.
169.1
Now, I can do the same thing for lists that have elements of different types in them. Remember, Python is a dynamically-typed language. So here I’m going to say hello and concatenate that to the string representation of whatever I’ve got in my list, which is going to be 1, false, and string jeremy.
197
Turn this whole map function into a list, and I get back hello 1, hello false, hello jeremy.
206.1
At this point, you’re probably feeling unhappy. Now you know Haskell, dynamically typed lists seem wrong. Let’s do a filter as well. Filter lamba x goes to x mod 2 equals 1.
229
Going to apply this over the range of integers from 1 to 10, not including 10. Again, if I convert this into a list, I can see the answer, which is 1, 3, 5, 7, 9. Actually, I could have used a list comprehension here instead– x for x in range 1, 10 if x mod 2 equals 1. And that returns the same value. For good measure, here’s a fold. I need to import the functools module. And now I can say functools dot reduce. This is the foldl function in Python.
277.7
Lambda a, b, a times b– that’s just a simple multiplication– over the range 1 to 5 with the starting value 1 gives me the product of 1 to 4 inclusive– effectively factorial of 4.
301.4
Other mainstream languages now incorporate lambdas as first-class functions. Examples include Java 8 released in 2014, and C++ 11, released in 2011. In the Comment section below, please let us know which languages you are using and whether you think there is scope to write in a functional style in these other languages.
324.2
Another thing to talk about is syntax. Now you know the clean and elegant syntax of Haskell, does Python seem clunky? Let us know in the Comments section.

All mainstream languages (like Python, Java and C++) are incorporating functional constructs. So even if you aren’t going to be using Haskell regularly, you can still develop your code in a more functional way in other languages.

Which languages do you use? How can you become more functional when you develop code in these languages? Please let us know in the comments section.

This article is from the free online

Functional Programming in Haskell: Supercharge Your Coding

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now