From: Alan M. Carroll
Subject: Scope, lambda expressions, and X3J13
Date: 
Message-ID: <1991Jul26.161500.28560@m.cs.uiuc.edu>
I'm not a Lisp sort of person, and I just am not getting the scoping
rules and how they apply to lambda functions. A common occurence is I
have an object A with a list L of other objects, and I want to have a
function that determines whether any of the things in L have property
P. To do this I need to use a function F that takes two arguments, an
object and a property. To me, the obvious solution seems to be
something of the form

(defun have-any-p (A P)
  (some `(lambda (obj) (F obj ,P)) (objects-of A))
)

where (objects-of A) returns the list L of objects.

This worked, but per the recent discussion here by BarMar about X3J13
and ANSI Lisp, I am concerned that this will fail under ANSI Lisp.
I've played around with other things, and I just can't get things to
work right. I am clearly missing something basic about scoping rules.
In particular, what can the lambda expression do with regard to P? Is
using backquote and inserting the value of P reasonable? Some of my
attempts have generated "Access to unbound symbol P".

My questions are

1. Is this likely to fail under ANSI Lisp?

2. If it's likely to fail, what should I do instead?

3. Would this work?

  (some (coerce `(lambda (obj) (F obj ,P)) 'function) (objects-of A))

-- 
Alan M. Carroll          <-- Another casualty of applied metaphysics
Epoch Development Team   
Urbana Il.               "I hate shopping with the reality-impaired" - Susan

From: Tim Moore
Subject: Re: Scope, lambda expressions, and X3J13
Date: 
Message-ID: <1991Jul26.105603.17895@hellgate.utah.edu>
In article <······················@m.cs.uiuc.edu> ·······@cs.uiuc.edu (Alan M. Carroll) writes:
>I'm not a Lisp sort of person, and I just am not getting the scoping
>rules and how they apply to lambda functions. A common occurence is I
>have an object A with a list L of other objects, and I want to have a
>function that determines whether any of the things in L have property
>P. To do this I need to use a function F that takes two arguments, an
>object and a property. To me, the obvious solution seems to be
>something of the form
>
>(defun have-any-p (A P)
>  (some `(lambda (obj) (F obj ,P)) (objects-of A))
>)
>
>where (objects-of A) returns the list L of objects.
>
>This worked, but per the recent discussion here by BarMar about X3J13
>and ANSI Lisp, I am concerned that this will fail under ANSI Lisp.
>I've played around with other things, and I just can't get things to
>work right. I am clearly missing something basic about scoping rules.

You really don't want to use backquote here. Use a function instead of
a lambda expression:
(defun have-any-p (A P)
  (some #'(lambda (obj) (F obj P)) (objects-of A)))

(Quick terminology check: a lambda expression is a list whose car is
LAMBDA; a function is a real functional object that is lexically
scoped within any outer functions.)

>In particular, what can the lambda expression do with regard to P?
The lambda expression can't do anything with P because it won't be
evaluated in have-any-p's scope.
 Is
>using backquote and inserting the value of P reasonable? 
Not really.

>My questions are
>
>1. Is this likely to fail under ANSI Lisp?
Yes.
>3. Would this work?
>
>  (some (coerce `(lambda (obj) (F obj ,P)) 'function) (objects-of A))
Yes, but it is inferior to the code I wrote above.

>-- 
>Alan M. Carroll          <-- Another casualty of applied metaphysics

-- 
Tim Moore                    ·····@cs.utah.edu {bellcore,hplabs}!utah-cs!moore
"Ah, youth. Ah, statute of limitations."
		-John Waters
From: Alan M. Carroll
Subject: Re: Scope, lambda expressions, and X3J13
Date: 
Message-ID: <1991Jul26.181335.4178@m.cs.uiuc.edu>
In article <······················@hellgate.utah.edu>, ·······················@cs.utah.edu (Tim Moore) writes:
> You really don't want to use backquote here. Use a function instead of
> a lambda expression:
> (defun have-any-p (A P)
>   (some #'(lambda (obj) (F obj P)) (objects-of A)))

Many thanks to Tom and others who sent email. The correct solution is
to use #', which I tested to verify that it does return #<Interpreted
Function> and not a lambda expression (at least under Allegro 4.0).

I think my problem stemmed from writing too much Emacs Lisp, which has
dynamic scoping (instead of lexical), so you have to use backquotes to
avoid name conflicts with intermediate functions (like 'some).

-- 
Alan M. Carroll          <-- Another casualty of applied metaphysics
Epoch Development Team   
Urbana Il.               "I hate shopping with the reality-impaired" - Susan