Subject: Re: how to define a local function
From: rpw3@rpw3.org (Rob Warnock)
Date: Thu, 12 Apr 2007 21:08:04 -0500
Newsgroups: comp.lang.lisp
Message-ID: <3cadndQ-0ueZe4PbnZ2dnUVZ_gadnZ2d@speakeasy.net>
Zach Beane  <xach@xach.com> wrote:
+---------------
| jayessay <nospam@foo.com> writes:
| > Does anyone remember the historical (or other) reason for why _labels_
| > is called "labels"?  It always seemed that the most likely name would
| > have been flet* (the above usage is particularly indicative of this in
| > comparison with let and let*).  Just curious, as I've never seen this
| > "explained"...
| 
| My turn to be picky. LABELS has an important difference from LET*: a
| LET* variable binding can refer only to preceding bindings, while
| LABELS can refer to any function binding, before or after, in the group.
+---------------

Right. Actually, Scheme gets it right for a Lisp1, with LET/LET*
and LETREC instead of CL's LET/LET*/FLET and LABELS. But LETREC
and LABELS have different syntaxes and semantics. LETREC binds
arbitrary *values*, not functions [though in a Lisp1 a "value"
which is an evaluated LAMBDA expression "is" a function], while
LABELS binds *only* functions, like FLET.

CL *could* have used FLETREC instead of LABELS, I suppose, except
LABELS was already in MacLisp (I think) and predated Scheme.

Note that even with LABELS, CL really has no direct counterpart
for Scheme's LETREC [unless you write a CL macro that expands it
the same way most Schemes do, as a LET that binds all the vars to
NIL first then SETFs them later]. That is, the following works in
Scheme, but there is no standard operator in CL that you can replace
the LETREC with that will work:

    > ((lambda (n)
	 (letrec ((f (lambda (x)
		       (if (<= x 1)
			 1
			 (* x (apply f (- x 1) '()))))))
	   (apply f n '())))
       5)

    120
    > 

[Note: The funny APPLY forms, which work in both Scheme & CL,
are a workaround for FUNCALL per se not existing in Scheme.]


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607