Popular conceit about learning programming languages

Lulu of the Lotus-Eaters mertz at gnosis.cx
Fri Nov 22 20:37:23 EST 2002


"Donn Cave" <donn at u.washington.edu> wrote previously:
|Probably the best counter example, of a language that emphasizes
|what I'm calling ``equational'' rather than procedural, is Haskell.
|It's the most prominent example of a ``non-strict, purely functional''
|programming language (``non-strict'' is a queer term that probably
|doesn't mean what you think.)

Haskell is really interesting.  It is a functional language that
nonetheless feels like it has the similar strengths and feel as Python.
Of course, I might be influenced in part by the superficial similarity
in syntax--both use indentation for blocks rather than noisy delimiter
symbols.

The term 'strict' has another antonym that is probably more useful:
'lazy'.  Of course, that word also has other meanings.  Almost all
imperative languages (procedural and OOP) are strict.  That is,
evaluating an expression involves evaluating every term in that
expression fully.  With lazy evaluation, only as much of an expression
as is needed is evaluated.  Boolean shortcutting is a limited example of
laziness; but Haskell goes much farther, basically everything is lazy.

For example, if you call a function, only those arguments that are
actually necessary to come up with the result are evaluated.  You do not
need to know in advance which arguments will be necessary, every
evaluation is deferred (but available) until it is actually used.

The even more striking example is with infinite lists.  You can easily
create a list with infinitely many items in Haskell... the trick is
that the specific membership is not calculated until a given item is
accessed.  For example, here is a way to calculate an infinite list of
every prime number.  As a bonus, the primality testing function
'isPrime' is thrown in:

    -- Define a list of ALL the prime numbers
    primes :: [Int]
    primes = sieve [2 .. ] -- Sieve of Eratosthenes
    sieve (x:xs) = x : sieve [y | y <- xs, (y `rem` x)/=0]
    -- Given an ordered list and a thing, is the thing in the list?
    memberOrd :: Ord a => [a] -> a -> Bool
    memberOrd (x:xs) n
      | x<n = memberOrd xs n
      | x==n = True
      | otherwise = False
    isPrime n = memberOrd primes n
    -- 'isPrime 37' is True
    -- 'isPrime 427' is False

The list comprehension statement should be familiar to Pythoneers, it
was an inspiration for the addition to Python.  The syntax is a little
closer to mathematics than Python's is, but it is easy to see the
similarity.

FWIW, generators in Python 2.2+ also allow a pretty easy way to
construct infinite lists.  They're not as automatically lazy as Haskell,
but you can get some of the same effects with a little more work.

Yours, Lulu...

--
    _/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: Postmodern Enterprises _/_/_/
   _/_/    ~~~~~~~~~~~~~~~~~~~~[mertz at gnosis.cx]~~~~~~~~~~~~~~~~~~~~~  _/_/
  _/_/  The opinions expressed here must be those of my employer...   _/_/
 _/_/_/_/_/_/_/_/_/_/ Surely you don't think that *I* believe them!  _/_/





More information about the Python-list mailing list