Good introduction to functional programming with Python?

Tim Peters tim.one at home.com
Sat Dec 23 01:45:59 EST 2000


[Alex Maretlli]
> 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].

I agree it depends on what the original questioner wants to get out of this.
That I don't know.  Haskell is a pure functional language with a wealth of
supporting material and easy-to-get, easy-to-use free ports, so it's a good
choice if FP is really what he wants.

If it is, then if FP is "about" anything, it's about the art of using
higher-order functions, and I believe mere mortals *need* a strong static
typing system to keep those straight.  Maps returning maps returning maps
... can get hard to follow real fast.

And I think anyone setting out to learn FP without a lazy language is
cheating themself out of a world of beauty:  learning to sling unbounded
streams in a *natural* setting is simply a joy.

If the guy just wants to putz around, though, I'd steer him to Scheme
<wink>.

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

I imagine that depends too on what he wants out of this.  He should start
with the "Gentle Introduction to Haskell" at

    http://www.haskell.org/tutorial/

and take it from there.

> 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.

The last is what I recommend to most people:  not too abstract, and very
careful to explicitly declare the types of all identifiers <wink>.

> ...
> On the other hand, if one's focus is on understanding how best
> to apply FP *in a Python setting*, his FP curriculum may prove
> too round-about -- ...

FP features in Python were never intended to be more than a minor
convenience, and by all signs will never become more than that.  So I steer
him away from that.  Don't forget that the strongest argument in favor of
listcomps (in Guido's eyes) was that they gave a path to eventually throwing
"map" and "filter" away!

> ...
> A NON-pure language one SHOULD consider for its didactical strength
> is Scheme.

Indeed closer to Python in many ways than is Haskell.

> 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 -- ...

Yup!  Pne of the few true programming classics.

> 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).

A caution that this depends on how powerful his computer is:  DrScheme is
too much a resource hog for a satisfying experience on older Windows boxes
(voice of experience).  This is a bit ironic, since Scheme is ported at
least as often as Python to tiny platforms.  You pay through the nose for
"all the trimmings" that come with DrScheme.  Worth it if you can afford it!
Else he can pick up implementations than run fine even on palmtops; e.g.,
from the links at (sorry, but my mailer will break this line):

http://www.dmoz.org/Computers/Programming/Languages/Lisp/Scheme/Implementati
ons/

betting-he's-got-more-advice-than-he-can-use-now-ly y'rs  - tim





More information about the Python-list mailing list