Newsgroups: comp.lang.scheme.c
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!news.sprintlink.net!noc.netcom.net!netcom.com!NewsWatcher!user
From: hbaker@netcom.com (Henry Baker)
Subject: Re: First Class procedures in C (was Abelson and Sussman)
Message-ID: <hbaker-0306950959060001@192.0.2.1>
Sender: hbaker@netcom4.netcom.com
Organization: nil
References: <WAND.95May30092623@delphi.ccs.neu.edu> <199505301429.KAA05167@bloom-beacon.MIT.EDU> <JRM.95May31121654@dragnet.packetclustr.com>
Date: Sat, 3 Jun 1995 17:59:06 GMT
Lines: 69

In article <JRM.95May31121654@dragnet.packetclustr.com>,
jrm@packetclustr.com (Joe Marshall) wrote:

>     bwp> Passing functions and procedures into other functions and procedures
>     bwp> is not as trivial in C, but easy none the less using function
>     bwp> pointers.
> 
>     >> Function pointers allow you to pass functions into other
functions, but they
>     >> are not as general as first-class procedures in Scheme, because
they do not
>     >> let you create new functions and return tham as the values of
procedures, as
>     >> in
>     >> 
>     >> (define twice
>     >> (lambda (f)
>     >> (lambda (x) (f (f x)))))
>     >> 
>     >> I've never seen this done convincingly in C.
> 
> Try these in C or C++:
> 
> (define (me) me)
> 
> (define (f list) (apply f (cdr list)))
> 
> >>>>> "Jim" == Jim Miller <jmiller@martigny.ai.mit.EDU> writes:
> 
>     Jim> I believe the actual issue is around the capture of free variables.
>     Jim> While C is lexically scoped and has first class procedures, it does
>     Jim> not allow a procedure to reference "dynamic free variables" (the old
>     Jim> language C inherited from its predecessor BCPL).  That is, C variable
>     Jim> references must be to either static variables or variables declared
>     Jim> within the innermost procedure.  This design decision allows C to
>     Jim> avoid creating closures (as Scheme systems do) and the concommittent
>     Jim> need for garbage collection.
> 
> While that is certainly part of the problem, the impoverished type system
> of C is a significant difficulty as well.  In the example above, F refers
> to a class of functions whose output and input are of the same
> (unspecified) type.  It returns a function of that accepts that type.  C
> (and C++ for that matter) have no way of representing this sort of
> animal**.  Similarly, recursive function types are disallowed in both C
and C++.
> 
> These two difficulties -- no dynamic free variables and extremely primitive
> non-polymorphic type system -- make higher order procedures essentially
> impossible.  You are not prohibited from making first class functions in C
> an C++, but you can't do anything with them once you have made them.
> 
>     Jim> If you want some *real* fun, take a look at the definition of C++ and
>     Jim> its scoping rules.  The amount of complexity added to the language to
>     Jim> avoid this same "problem" (i.e. the need for a garbage collector) is
>     Jim> just amazing!
> 
> And they failed, despite it all!  Programs either `leak' or there are a
> number of ad-hoc reference count GC's sprinkled about the program.

You may find my 'Cheney on the MTA' scheme interesting for implementing
function closures in C/C++.  See:

ftp://ftp.netcom.com/pub/hb/hbaker/CheneyMTA.html  (also .ps.Z)
and
ftp://ftp.netcom.com/pub/hb/hbaker/cboyer13.c

-- 
www/ftp directory:
ftp://ftp.netcom.com/pub/hb/hbaker/home.html
