Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Skip to 0 minutes and 5 secondsJEREMY: 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.

Skip to 0 minutes and 20 secondsShock. 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.

Skip to 1 minute and 16 secondsHere, 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.

Skip to 1 minute and 48 secondsThe 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.

Skip to 2 minutes and 7 secondsHere's another map function.

Skip to 2 minutes and 15 secondsHello 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.

Skip to 2 minutes and 49 secondsNow, 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.

Skip to 3 minutes and 17 secondsTurn this whole map function into a list, and I get back hello 1, hello false, hello jeremy.

Skip to 3 minutes and 26 secondsAt 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.

Skip to 3 minutes and 49 secondsGoing 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.

Skip to 4 minutes and 38 secondsLambda 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.

Skip to 5 minutes and 1 secondOther 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.

Skip to 5 minutes and 24 secondsAnother 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.

Functional Programming in Other Languages

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.

Share this video:

This video is from the free online course:

Functional Programming in Haskell: Supercharge Your Coding

University of Glasgow

Get a taste of this course

Find out what this course is like by previewing some of the course steps before you join:

  • Haskell history poster
    Brief History of Haskell

    What are the origins of the Haskell programming language? In this article, Dr Jeremy Singer explores the history of Haskell.

Contact FutureLearn for Support