To get a taste of functional programming, we took a quick dive into Racket, a modern dialect of Lisp.


Python is a programming language that supports "first-class functions." In such a language, functions can be assigned to variables, which can then be passed in as arguments to other functions. For example, Python has a built-in function map, whose input parameters are a function and a list of numbers. The function will return a new list, consisting of the values obtained by applying the function to the numbers in the original list. So for the function

def addTen(x):

return x+10

which adds ten to a number, the statement newlist = map(addTen, [ 1, 2, 3 ] ) will create a new list, containing the numbers 11, 12, and 13.

"Functional programming" is a style of programming that has become increasingly popular in recent years, and I felt it would be worthwhile to give my Intermediate Python students a little taste of what functional programming is all about. So at the end of Block Four, we spent some time investigating some of the functional programming ideas that Python supports, such as first-class functions, and "anonymous functions," which are function definitions that are not bound to any variable name.

However, Python is not a "pure" functional programming language. One of the features of a "pure" functional programming language is that writing loops is either impossible or frowned upon from a philosophical point of view. Instead, recursive functions are used to iterate over collections of data. I wanted my students to see what such a programming language is like, so at the start of Block Five, we left the land of Python and spent two weeks learning the basics of a language called "Racket", a modern dialect of Lisp.  

Racket differs from Python both syntactically and stylistically. In Racket, all arithmetic expressions are written in prefix notation with full parentheses, so that, instead of writing  2 + 3, you write ( + 2 3 ). That takes a while to get used to! More importantly, recursive functions are used to perform all the most common operations on lists. For example, to find the sum of a list of numbers, you add the first number in the list to the recursive call that gives you the sum of the numbers in the rest of the list. People who believe in this style of programming truly believe that this is the "right" way to code. Recently I encountered this quote from one of the gurus of the field:

Loops per se are evil -- opium for the masses if you so wish

Now that we have reached the end of this unit, I must say that many of my students did not buy into the ideas of functional programming and will be more than happy to return to the more familiar land of Python for the rest of the school year. However, I believe that thinking about programming in this way is a worthwhile intellectual exercise that will serve my students well, especially if they plan to study computer science at a more advanced level in the future.  

--Steve Gregg