newbie conceptual question
Wed, 25 Jul 2001 16:46:19 +0200
> I have been programming with imperative languages for 15 years. Now I
> started learning Haskell, and functional programming (and I am very
> determined to learn it well). It seems to me that I can still use functional
> programming paradigm with an imperative language. How can I benefit more
> from a functional programming language (i.e.Haskell)? In other words, is
> having built-in language constructs for functional programming the only
> reason for using a functional programming language(I can use polymorpim, and
> generic programming in imperative languages, as well).
I'm sure that the regular posters will have a ream of intelligent, informed
comment to make about this. Just for once, though, I think there's space
for a comment by a functional-programming tyro, precisely because I'm
closer to being in your shoes than, say, Philip Wadler is. Just don't
assume I'm right (but no doubt the experts will let us know if I'm wrong.
Have a mallet, guys). I've appended to this message the names of a few
publications that I particularly like.
Disclaimer: the only purely functional language that I know is Haskell (and
a negligible smidgen of ML).
Point One: functional languages are powerful. The phrase 'built-in
language constructs for functional programming' doesn't begin to describe
it. Most imperative programmers don't even imagine the degree of
abstraction and generality these languages afford. Polymorphism and
generic programming are, as you say, possible in imperative languages, even
if they're not always explicitly supported by the language. Likewise
garbage-collection and (sort-of) higher-order functions. But I say: throw
in lazy evaluation, -real- higher-order functions, currying and referential
transparency, and find out what generality and abstraction really mean.
Point Two: functional languages are beautiful. This brings tangible
benefits as well as aesthetic satisfaction: unless you're doing something
really dirty, your Haskell program will usually be more concise, explicit
and general than its (say) Java equivalent -- often much more so. Hence
readability and maintainability. Moreover, I've found learning Haskell to
be a small education in itself. It's a truism that a language is only
worth learning if it changes the way you think about programming; and
though I still mostly program in imperative languages, a functional flavour
has crept into much of my work. Even my Perl now tends towards recursion
and non-destructive assignment, and I think this makes me a better
Point Three: functional languages are robust; or, rather, they very
strongly encourage robust programming. This (along with Haskell's
delicious typing system) is mainly because they do not permit data to be
updated: if you've referred to a datum in the past, you can guarantee that
it will not differ the next time you look at it. (Yes, this -does- give C
programmers the heebie-jeebies. No, programs can still do useful things.)
Hence referential transparency: given the same arguments, a functional
function will always return the same answer. To debug an imperative
program, as my lecturer used to say, you have to understand the entire
history of the computation; in a functional program, whatever's going on is
guaranteed to be no more complex than the particular function you're in.
Functional programs rarely need debugging; when they do need debugging, the
difficulty of the job is of a lower order of complexity than debugging an
imperative program. Try it and you'll see.
But, of course, every powerful force has its Dark Side.
Point Four: functional languages are difficult are learn. The experts tend
to put this statement in incredulous quotes, but I assert that it is still
true if one is trying to learn Haskell on one's own without a grounding in
certain mathematical principles that underlie the language. I was taught
the basics by a damn' good lecturer, but most of my functional education
has been self-propelled -- and frustrating. I've read some very good
books, but various important concepts continue to elude me even though I've
spent a fair bit of time meddling around with Haskell. What, for instance,
the hell are monads? Though I've read, and pondered, and though I use the
Haskell I/O system without difficulty, I still don't really grok monads
except to believe that they'd make a lot of things easier if I did
understand them. Presumably I'm missing some fundamental insight, probably
a simple one, but what? I don't know.
Point Five: there's a dearth of libraries. Functional languages lack the
embarrassing plethora of libraries that come with C++ or Java or Perl. For
instance, there are simply are no mature, maintained Haskell libraries for
GUI development or database access (as far as I know -- I haven't checked
haskell.org in a while). So I end up using Perl::Tk and DBI.pm instead,
because the job needs to be done today. (By the way, I understand that
other functional languages (Clean, ML (and Erlang?)) are better equipped.)
In summary, I love Haskell. I think it knocks the socks off any other
language I've learned. When writing Perl, my frequent refrain is, "This'd
be so much easier in Haskell!" and when programming in Haskell the cry is,
"Yum!" But all too often I stray out of the domain of pure algorithm and
have to cope with real-world grunginess. Then I growl in despair of ever
getting to grips with Haskell, and all too often take refuge with one of
the mainstream imperative languages (or, in extreme cases, Jack Daniels).
Yowch, I've written a monograph. I'd better get back to my Perl before
anyone notices me goofing off.
David Hughes -+- office: +41 22 767 4047
UNIX Sysadmin, SERCo SA -+- mobile: +41 79 201 4732
Computing Centre, CERN -+- David.W.Hughes@cern.ch
This message may not represent the views of my employer or of CERN.
I recommend the following reading-matter:
Simon Thompson, "Haskell: the craft of functional programming",
Paul Hudak, "The Haskell School of Expression", Cambridge Univ. Press
... and anything linked from http://www.haskell.org/bookshelf, but
Philip Wadler, "Why no one uses functional languages"
Philip Wadler, "What The Hell Are Monads?"