[Tutor] Re: List Comprehensions again

dman dsh8290@rit.edu
Fri, 25 Jan 2002 18:20:14 -0500


On Thu, Jan 24, 2002 at 11:52:05PM -0500, Erik Price wrote:
 
| As a superfledgling, I have to say that I have no idea what you guys are 
| talking about.  For the first few messages I figured it was something 
| that I could pick up later on.  Now this thread has gone on for several 
| days and I'm starting to wonder if I should learn this lambda / list 
| comprehensions thing now before I run into the troubles that others 
| describe (who seem to be saying that "they wish they'd learned it a 
| different way" -- I want to learn it that way first so I can save the 
| hassle).
| 
| Where should I look for more info, or is this something that can wait 
| until I am more comfortable with the language?  (I am just finishing 
| tutorials on syntax so you can see that I am really really green at 
| this.)

You can wait on these features for now.  For a really brief, but
hopefully helpful, overview :

map, filter, and reduce are functions that take a function object and
a list object as arguments.  The first two return a new list, the
third returns something else (depends on what is in your list and the
function object).  The next step for you to do is to see what these
functions do with their arguments and how the return value relates to
the arguments.

lambda is a keyword that creates a simple, unnamed, function object.
It is "simple" because it can only contain a single expression.  For
example, the following are equivalent :

def f( x ) :
    return x*x

f = lambda x : x*x

lambda is most often used in conjunction with map/filter/reduce, and
the whole lot comes from the "functional" programming style (embodied
in LISP, Scheme, and the ML family of languages).

List comprehensions are another way of doing the same thing as map or
filter.  LCs are shorter, syntactically, and appear more mathematical.

HTH,
-D

-- 

A wise servant will rule over a disgraceful son,
and will share the inheritance as one of the brothers.
        Proverbs 17:2