From: John Tu
Subject: What is the meaning of "supporting first-class functions" ?
Date: 
Message-ID: <01bd810b$809ae4a0$229abfcc@nameserver.sfu.ca>
While doing my "Comparative Programming Languages" homework, I need to know
the exact definition of "first-class functions". But I could not find it in
all my textbooks and reference books. Who can help me out?
From: Kent M Pitman
Subject: Re: What is the meaning of "supporting first-class functions" ?
Date: 
Message-ID: <sfwpvhddfog.fsf@world.std.com>
"John Tu" <ยทยทยท@istar.ca> writes:

> While doing my "Comparative Programming Languages" homework, I need to know
> the exact definition of "first-class functions". But I could not find it in
> all my textbooks and reference books. Who can help me out?

The following is not from any book, and might or might be the answer
you're seeking.  So don't stop reading around in your books just on my
say-so, but here's what I have always interpreted this to mean:

In some languages, functions are not objects at all.  That is, there is
no way to actually get one into your hands to store in a variable or
pass to another function.  So you might write a definition of a function
  F(X,Y)=X*2+Y
but the only thing you can do with the function is call it, as in
  F(3,4)
In languages like this, you can't do G(F) nor can you do
 F(G,A,B)=G(A,B)

In some languages, functions are "second-class" in that you can get at them
in some circumstances but not in others. e.g.,

 F (X) = 
   {  G (X) = X*2+1;
      RETURN H(G,X) }
 H (G,A) = G(A)+G(A+1)

In this case, G is passed to a function H where it is used.  But what makes
it second-class is that you CANNOT do this:

 F (X) =
   {  G (X) = X*2+1;
      RETURN H(G,X) }
 H (G,A) = 
   { Z (X) = G(A)+G(A+1);
     RETURN Z }

That is, you can't return a function beyond the lexical contour where
it was created. You can use a function, like you can use a variable,
anywhere in the creation block or any subblock of that block.  But
you cannot return it because upon deactivation on the frame in which
the function was created, the function ceases to have meaning.

In a first-class language, you CAN do the last example above because
the definition of G(X) and Z(X) associate G and Z with objects that are
as tangible and persitent (and hence "first class") as any other data
object such as a number.  In the case of both G and Z, these functions
are also closures over various lexical variable values, but that's an
orthogonal issue.

This is probably not the best explanation you'll ever see, but I hope
it helps a little.