Skip to 0 minutes and 5 seconds 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.
Skip to 0 minutes and 20 seconds 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.
Skip to 1 minute and 16 seconds 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.
Skip to 1 minute and 48 seconds 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.
Skip to 2 minutes and 7 seconds Here’s another map function.
Skip to 2 minutes and 15 seconds 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.
Skip to 2 minutes and 49 seconds 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.
Skip to 3 minutes and 17 seconds Turn this whole map function into a list, and I get back hello 1, hello false, hello jeremy.
Skip to 3 minutes and 26 seconds 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.
Skip to 3 minutes and 49 seconds 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.
Skip to 4 minutes and 38 seconds 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.
Skip to 5 minutes and 1 second 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.
Skip to 5 minutes and 24 seconds 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.
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.
© University of Glasgow