From: Richard A. O'Keefe
Subject: #' in quoted data
Date: 
Message-ID: <10735@goanna.cs.rmit.oz.au>
I was explaining to someone how Lisp syntax (what there is of it (:-)) works,
and then I suddenly ran up against a blank spot in my understanding.
The mental model I have for sharp-quote is "#' is something you put inside
an expression to compensate for the two-name-space mistake".  That is, I
have been thinking of #' as a piece of _expression_ syntax.  But it is not
a special form at all, it is reader syntax.  So I find myself wondering how
something like
	(let ((x 10))
	    '(( fetch . #'(lambda () x ) )
	      ( store . #'(lambda (y) (setf x y)) )))
is supposed to work.  The only approach I could think of is to keep track
of the levels of ` and ' you're inside, and treat
	'( ... #'-foo- ... )
as if it had been
	`( ... ,#'-foo- ... )
which would work for quoted data in expressions, but still wouldn't work for
data being (read) from a file.

So how do lexical scoping, #', and ' work together?

By the way, this question is not uniquely my own.  I just tried this in
a system which is supposed to be based on CLtL1, and the value I got was
((FETCH FUNCTION (LAMBDA () X)) (STORE FUNCTION (LAMBDA (Y) (SETF X Y)))
which is not terribly useful.  (Even if these lists do act like functions,
they have lost their environments.)

-- 
I am writing a book on debugging aimed at 1st & 2nd year CS students using
C/Modula/Pascal-like languages.  Please send suggestions (other than "you
_must_ cite "C Traps and Pitfalls") to ··@goanna.cs.rmit.oz.au

From: Sandra Loosemore
Subject: Re: #' in quoted data
Date: 
Message-ID: <1992May18.120827.7814@cs.yale.edu>
Richard O'Keefe wants to know how to put a closure in quoted data, like

	   (let ((x 10))
	       '(( fetch . #'(lambda () x ) )
		 ( store . #'(lambda (y) (setf x y)) )))

The fundamental problem here is that the functional expression has to
be evaluated in order to yield a closure, and the arguments of QUOTE
expressions are not evaluated at all.  So the #' forms in this example
are just constant lists of the form (FUNCTION (LAMBDA ...)).

The solution is to use backquote instead of quote in this example to
build the a-list; use ,#'(lambda ...)  to force evaluation of the
functional expressions.  In a data file, you could use #. to force
read-time evaluation of the functional expressions.  Again, the key is
that you have to evaluate functional expressions *somewhere* to turn
them from s-expressions to function objects.

-Sandra
From: Richard A. O'Keefe
Subject: Re: #' in quoted data
Date: 
Message-ID: <10758@goanna.cs.rmit.oz.au>
In article <·····················@cs.yale.edu>, ················@CS.YALE.EDU (Sandra Loosemore) writes:
> Richard O'Keefe wants to know how to put a closure in quoted data, like

> 	   (let ((x 10))
> 	       '(( fetch . #'(lambda () x ) )
> 		 ( store . #'(lambda (y) (setf x y)) )))

No.  That is _not_ what I asked.  I ***know*** how to do that, and indeed
I ***explained*** how to do that in my posting.  (Treat #' as ,#' and use
` instead of ').  I understand about backquote (thanks to CLtL2).

My question was:
	Common Lisp S-expression syntax ALLOWS sharp-quote in quoted data.
	The expression above _appears_ to be legal Common Lisp syntax.  At
	least one Common Lisp implementation accepts it.
	WHAT DOES IT MEAN?   And how does that work?

I'll say it again:  if I want to put a closure inside quoted data, I already
know how to use backquote to do it.  But if I am implementing (READ) what am
I supposed to _do_ when I see something like '(A #'FOO B) (bearing in mind
that FOO may be locally fbound).
-- 
I am writing a book on debugging aimed at 1st & 2nd year CS students using
C/Modula/Pascal-like languages.  Please send suggestions (other than "you
_must_ cite "C Traps and Pitfalls") to ··@goanna.cs.rmit.oz.au
From: Barry Margolin
Subject: Re: #' in quoted data
Date: 
Message-ID: <v8l5nINNlr7@early-bird.think.com>
In article <·····@goanna.cs.rmit.oz.au> ··@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
>I was explaining to someone how Lisp syntax (what there is of it (:-)) works,
>and then I suddenly ran up against a blank spot in my understanding.
>The mental model I have for sharp-quote is "#' is something you put inside
>an expression to compensate for the two-name-space mistake".  That is, I
>have been thinking of #' as a piece of _expression_ syntax.  But it is not
>a special form at all, it is reader syntax.

Someone else already explained how to recode your example correctly using
backquote, but I don't think he really addressed your confusion completely.

Yes, #' is reader syntax, but it's just a shorthand for expression syntax.
#'FOO is an abbreviation for (FUNCTION FOO), which is a special form.
That's why it has to be evaluated in order to work.

' is similar: 'FOO is just shorthand for (QUOTE FOO).  If you put a quoted
expression inside a quoted list, you'll see the literale QUOTE symbol when
you look in the list; e.g. evaluating '(FOO 'BAR BAZ) results in (FOO
(QUOTE BAR) BAZ).

Some reader macros do more than just generate forms that need to be
evaluated, so it's easy to understand why you would be confused.  For
example, #S(FOO ...) and #A(...) actually expand into the structure and
array objects, not into (MAKE-FOO ...) or (MAKE-ARRAY ...) forms.

#' can't reasonable expand into the function object itself, because it is
expanded at read time.  In order for it to have access to lexical function
bindings, its expansion must be evaluated in the appropriate lexical
environment.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar
From: Erann Gat
Subject: Re: #' in quoted data
Date: 
Message-ID: <1992May18.162312.8079@elroy.jpl.nasa.gov>
In article <·····@goanna.cs.rmit.oz.au> ··@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
>So how do lexical scoping, #', and ' work together?
>
>By the way, this question is not uniquely my own.  I just tried this in
>a system which is supposed to be based on CLtL1, and the value I got was
>((FETCH FUNCTION (LAMBDA () X)) (STORE FUNCTION (LAMBDA (Y) (SETF X Y)))
>which is not terribly useful.  (Even if these lists do act like functions,
>they have lost their environments.)

#' is reader syntax which expands as follows:

#'expression <==> (function expression)

FUNCTION is a special form which takes as an argument either a symbol or
a list whose car is LAMBDA and returns a function object.  (The rhetoric
is that FUNCTION converts names of functions into the functions themselves,
and that LAMDBA lists are the names of anonymous function.)  So in order
to do what you want you have to arrange for the FUNCTION expression
(i.e. the #' expression) to be evaluated at some point.  There are two
ways to do this.  One is to use the #. reader syntax which forces evaluation
at read time, and the other is to use backquote, which is probably the
preferable solution.  So you could do somthing like:

(let ( (x nil) )
  ...
  `(fetch ,#'(lambda (...) ... x ...)))

>I am writing a book on debugging aimed at 1st & 2nd year CS students using
>C/Modula/Pascal-like languages.  Please send suggestions (other than "you
>_must_ cite "C Traps and Pitfalls") to ··@goanna.cs.rmit.oz.au

My biggest suggestion: use Scheme instead.  First and second year students
have enough to worry about without having their minds bent by the weight
of historical and syntactic baggage.  (IMHO)


Erann Gat
···@robotics.jpl.nasa.gov
From: Richard A. O'Keefe
Subject: Re: #' in quoted data
Date: 
Message-ID: <10759@goanna.cs.rmit.oz.au>
In article <·····················@elroy.jpl.nasa.gov>, ···@forsight.jpl.nasa.gov (Erann Gat) writes:
> #' is reader syntax which expands as follows:

> #'expression <==> (function expression)

Ah.  I know about FUNCTION.  What I hadn't realised (I have 3 copies of
CLtL1 and 1 copy of CLtL2, but have moved office, and now need a salvage
librarian (:-)) was that the expansion of #' was so tightly constrained.

To think that I had a design nearly finished which would make lexically
scoped #' inside ' work the way I expected...  

> >I am writing a book on debugging aimed at 1st & 2nd year CS students using
> >C/Modula/Pascal-like languages.  Please send suggestions (other than "you
> >_must_ cite "C Traps and Pitfalls") to ··@goanna.cs.rmit.oz.au
> 
> My biggest suggestion: use Scheme instead.  First and second year students
> have enough to worry about without having their minds bent by the weight
> of historical and syntactic baggage.  (IMHO)

For what it's worth:  I agree and have been pushing for the adoption of
Scheme here.  This might be of interest to readers of comp.lang.lisp:  when
the rumour that we were considering a possible switch to Scheme as first-
year language as maybe something we might conceivably do in the indefinite
future reached the schools that provide our local students, we got several
horrified telephone calls to the effect that if we were going to go all
abstract and academic like that the schools would have to advise their
students to go somewhere else that would teach _real_ computer languages.
If you've been around for a while, this will sound familiar; time was when
Pascal wasn't a real language either.  First one must teach the teachers...
In the mean time, there are 1st & 2nd year CS students here and elsewhere
using C, Modula-2, or some flavour of Pascal, and a lot of them haven't the
remotest idea what to do when something goes wrong in their program.

-- 
I am writing a book on debugging aimed at 1st & 2nd year CS students using
C/Modula/Pascal-like languages.  Please send suggestions (other than "you
_must_ cite "C Traps and Pitfalls") to ··@goanna.cs.rmit.oz.au