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 ob
OO is about nouns while functional programming is about verbs. Read this rather good blog on the subject by Steve Yegge.
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.
Try looking at this article called Functional Programming for the Rest of Us, below is an excerpt.
In this article I will explain the most widely used ideas from functional languages using examples written in Java (yes, you could write functional programs in Java if you felt particularly masochistic). In the next couple of sections we'll take Java as is, and will make modifications to it to transform it into a useable functional language. Let's begin our quest.
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.
We used this tutorial for Haskell, and it worked very well, its light hearted but does a good job with explanations:
Learn you a Haskell for Great Good
The audience would be people with little programming experience,
Is it really possible to explain functional programming, let along OO or procedural or any paradigm of programming to people without much programming experience?
or people who only have object-oriented 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:
using System;
namespace Juliet
{
interface IConvertor<T, U>
{
U Convert(T value);
}
class Program
{
static U[] Convert<T, U>(T[] input, IConvertor<T, U> convertor)
{
U[] res = new U[input.Length];
for (int i = 0; i < input.Length; i++)
{
res[i] = convertor.Convert(input[i]);
}
return res;
}
class SquareInt : IConvertor<int, string>
{
public string Convert(int i)
{
return (i * i).ToString();
}
}
class ScaleInt : IConvertor<int, string>
{
readonly int Scale;
public ScaleInt(int scale)
{
this.Scale = scale;
}
public string Convert(int i)
{
return (i * Scale).ToString();
}
}
static void Main(string[] args)
{
int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
string[] squared = Convert<int, string>(nums, new SquareInt());
string[] tripled = Convert<int, string>(nums, new ScaleInt(3));
}
}
}
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 IConvertor<T, U>
class is just a wrapper around a single function called Convert
-- the class literally exists to help us pass Convert
to other functions. If you can understand this much, then you already understand the basic principles behind functions as first-class values -- functional programming is all about passing functions to other functions in pretty much the same way you pass a person or an int or a string.
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:
using System;
namespace Juliet
{
class Program
{
static U[] Convert<T, U>(T[] input, Func<T, U> convertor)
{
U[] res = new U[input.Length];
for (int i = 0; i < input.Length; i++)
{
res[i] = convertor(input[i]);
}
return res;
}
static string SquareInt(int i)
{
return (i * i).ToString();
}
static void Main(string[] args)
{
int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
string[] squared = Convert<int, string>(nums, SquareInt); // pass function by name
string[] tripled = Convert<int, string>(nums, i => (i * 3).ToString()); // or pass 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:
class FileFunctions
{
internal void SaveFile()
{
SaveFileDialog fileDialog = new SaveFileDialog();
fileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
File.AppendAllText(fileDialog.FileName, MyDocument.Data);
}
}
internal void WriteFile()
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
MyDocument.Data = File.ReadAllText(fileDialog.FileName);
}
}
}
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:
class FileFunctions
{
internal void ShowDialogThen(FileDialog dialog, Action<FileDialog> f)
{
dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
f(dialog);
}
}
internal void SaveFile()
{
ShowDialogThen(
new SaveFileDialog(),
dialog => File.AppendAllText(dialog.FileName, MyDocument.Data));
}
internal void WriteFile()
{
ShowDialogThen(
new OpenFileDialog(),
dialog => { MyDocument.Data = File.ReadAllText(dialog.FileName); });
}
}
Awesome.