Good introduction to functional programming with Python?

Alex Martelli aleaxit at yahoo.com
Fri Dec 22 03:50:56 EST 2000


"Tim Peters" <tim.one at home.com> wrote in message
news:mailman.977465166.28872.python-list at python.org...
> [Richard P. Muller]
> > Can someone recommend a decent introduction to functional programming
> > in Python?
>
> I doubt it, but if you want to learn FP I strongly recommend checking out
> the freely available Haskell material:
>
>     http://www.haskell.org/

http://sourceforge.net/projects/xoltar-toolkit/ has Python utilities
for better support for some Haskell-like functional programming in
Python, including the key Haskell semantic idea, lazy evaluation.

Haskell is my favourite FP language, and it does have some nice
surface similarities with Python (use of whitespace, the list
comprehension idea), but it differs deeply from Python in two
key issues (which are NOT really central to FP...):

    a. lazy evaluation is always the default semantics
    b. very strict (and elegant) static typing

Python evaluates eagerly by default (like almost all languages!),
and its typing is always dynamic.

If one wants to broaden one's mind, looking at the world from a
very different angle than Python's, then Haskell makes a LOT of
sense.  A 'triangulation' is best done by choosing two 'base
points' that are not TOO close to each other, and nobody would
accuse Haskell and Python of being too close, given [a] & [b].

Incidentally, while haskell.org is a great site, I would strongly
suggest that books are by far the best way to learn Haskell.

Hudak's "Haskell School of Expression" (Cambridge University Press)
may well wow you if you're into multimedia & such; Thompson's more
traditional "Craft of Functional Programming" 2nd ed (Addison
Wesley) is workmanlike, thorough, and clear.  From there you
can move on to Fethi's and Lapalme's "Algorithms: a Functional
Programming Approach" (Addison-Wesley) and Okasaki's "Purely
Functional Data Structures", and finally HAVE a good grasp of
FP the Haskell way (Okasaki's work actually focuses on ML, with
Haskell equivalents in an appendix, so you'll be getting a small
apercu of non-lazy FP thrown in for free:-).

On the other hand, if one's focus is on understanding how best
to apply FP *in a Python setting*, this FP curriculum may prove
too round-about -- the conceptual detours through static typing
and "laziness as a way of life", in particular, are surely
scenic and mind-broadening, but not specifically relevant.  There
are faster and more direct ways...


> > I haven't found any suitable intro, and I'd like to learn more
> > about map(), apply(), lambda, and the like.
>
> Haskell will teach you that.  You can come back and apply what you learned
> in Python then -- although some people will cluck at you <wink>.
>
> > I understand the syntax, but I'd like to learn something of
> > the functional programming context these commands come from.
>
> If you want to learn French, don't go to Manchester.  Functional
programming
> features are not central to Python -- its support for them is spotty,
clumsy
> and unlikely to get fancier.  Learn FP in a context where it's natural!
And
> Haskell is the most Pythonic of all languages that have nothing in common
> with Python.

There must indeed be some Pythonic 'aura' to Haskell, I guess, given
the somewhat surprising correlation between liking Haskell and liking
Python among people who've tried both.  But points [a] and [b] above
are BIG technical differences, and not centrally relevant to FP.

There ARE functional programming languages with dynamic typing AND
eager (strict) evaluation models like Python.  A 'pure' FP with these
characters is up-and-coming Erlang -- but I would not necessarily
suggest it here, because its central semantic concern is with issues
of parallelism and concurrency, which have relatively little to do
with the issue.  A NON-pure language one SHOULD consider for its
didactical strength is Scheme.  Abelson's and Sussman's "Structure
and Interpretation of Computer Programs" will teach one Scheme in
a strongly FP-oriented setting, and it's a great book on its own --
I consider SICP as one of the main conceptual motivation to learn
a little Scheme, just as Kernighan and Pike's "The Practice of
Programming" is motivation enough to learn some C, or Fowler's
"Refactoring" motivates one to pick up 'just enough Java to get by'.

Scheme's syntax will feel pretty alien, what with all the darned
parentheses, but, with Abelson and Sussman at hand, one soon
gets past that and focuses on really deep and fascinating concerns.

http://www.cs.rice.edu/CS/PLT/packages/drscheme/ gives a free
and very good implementation, with all the trimmings and a lot
of equally-free didactical support, good for any Win32 box, Mac,
or Unixoid.  (Not that Haskell's and Erlang's implementations
are any less free, cross-platform, and good-quality, but, IMHO,
Rice's Scheme has characteristics that make it even better).


Alex






More information about the Python-list mailing list