What are some good examples that I can use to explain functional programming?
The audience would be people with little programming experience, or people who only have object-oriented experience.
What are some good examples that I can use to explain functional programming? The audience would be people with little programming experience, or people who only have object-oriented experience.
| |||||||||
feedback
|
Is it really possible to explain functional programming, let along OO or procedural or any paradigm of programming to people without much programming experience?
Well probably the best examples would be converting known design patterns into their functional equivalent. Let's take the canonical example of converting a list of ints to a list of strings:
Its simple, readable, object-oriented, its even generic so it works for any arbitrary types, so what's the problem? For a start, its bloated: I have an interface definition and two interface implementations. What if I need another conversion? Well, I need another interface implementation -- it can get out of hand quickly. When you think about it, the People usually prefer functional programming because it helps them avoid single-method interfaces and implementations. Instead of passing a class, we just pass a function by name or anonymously:
Alright, so now we have the exact same program in much fewer lines of code, its readable, and its very evident what its doing. Now someone might say "that's a neat trick, but when would I use it" -- there are plenty of cases when you'd want to pass functions around like this. It gives you a lot more power to abstract your programs control flow in novel ways. Adapted from an example shown here, consider a class which handles files:
Yuck. The code is repetitious, but its performing operations on different classes and invoking different methods. How would you abstract away the duplicated code in the OO universe? In functional programing, it's trivial:
Awesome. | |||||
feedback
|
Functional Programming for the Object-Oriented Programmer by by Brian Marick. I'm not associated, or anything, just came by it right now: Uncle Bob likes it! :) | |||
feedback
|
The ones with little programming experience are probably easier, since they won't have as many programming concepts to get in the way: functional programming is very similar to mathematical functions as expressions, so show them some math. For the OO crowd, you could show how closures are like encapsulation, and thus how functional programming encompasses OOP. You could also show how useful higher-order functions are, in particular how they cause some OOP patterns (such as the Strategy and Template patterns, and the Visitor pattern in languages with multimethods) fade away into the language (Peter Norvig asserts that 16 of the 23 GOF patterns are simpler in Dylan and Lisp). As a counterpart, message-passing based OOP (rather than the CLOS approach) is a pattern in FP that is invisible in OOP. Demonstrate how easy it is to write general arithmetic expressions, along with function composition and differentiation, as programming constructs and you've got both of them. You may or may not want to show the OO people CLOS's multimethods; they'll either riot or have a geekgasm. Either way, it's likely to be messy. Some other possibilities to show how flexible FP is: lazy evaluation can be implemented using nullary anonymous functions. FP can support both class-based and prototype-based OOP. There are plenty of other examples (e.g. continuation-passing style), but they generally require familiarity with FP before learning. SICP has plenty of interesting examples and problems; it should prove inspirational. | ||||
feedback
|
Compare it to sentence structure. I hit the ball, catch the ball, throw the ball: OO | |||||
feedback
|
Immutable data structures You know, I've commented to people about functional programming before and have brought up the whole "immutable data structure" thing. This usually blows people's mind -- how are you supposed to add something to a collection if you can't actually add it? The immediate answer is "well you don't, you just create a new version of your data structure with the object added", and the immediate response is "that doesn't sound very efficient". Well it is. Immutable stack Canonical immutable data structure is an immutable stack, which is basically a node containing two readonly values: a head and a tail. Pushing and popping from the stack are O(1). Indexed lookups, inserts/deletes in the middle of the list require O(n) time, which incidentally is the same as a mutable stack.
Immutable AVL Tree Most people aren't impressed by stacks because it isn't a "serious" data structure, but AVL trees and other self-balancing data structures are pretty impressive. Trees are actually very easy to write as immutable data structures. This version supports O(log n) inserts and lookups, delete isn't shown but it can be implemented in O(log n) as well. In other words, the immutable AVL tree has the same computational complexity as as its mutable variant:
| |||
feedback
|
Why Functional Programming Matters by John Hughes has some good examples, but even better, it explains the why of functional programming languages, not just the how. You could do much worse than build a presentation based on that paper! | |||
feedback
|
OO is about nouns while functional programming is about verbs. Read this rather good blog on the subject by Steve Yegge. | |||||
feedback
|
We used this tutorial for Haskell, and it worked very well, its light hearted but does a good job with explanations: | |||
feedback
|
I don't think you can, frankly. Functional programming (like 'good' OO programming, vs. 'procedural with classes') requires a mental model shift. You just can't understand it as a procedural coder without actually taking the time and working through using it. If you really want someone to understand functional programming, get them a copy of the Little Schemer or some other equivalent book, and have them sit down and work through the exercises. Nothing else will help them build the necessary mental model. | |||
feedback
|
It might be easier to describe functional programming by its fundamental characteristics first:
Of these, the most important characteristic is the notion of no side effects, as the other characteristics follow from that. Functional programming is used in places you might not expect. It is used to run traffic lights and communications switches (Ericsson switches run Erlang), for example. | ||||
feedback
|
Try looking at this article called Functional Programming for the Rest of Us, below is an excerpt.
| |||
feedback
|