#'(lambda ... (lambda 
Author Message
 #'(lambda ... (lambda

I was wondering why this:

(setf (symbol-function 'double) #'(lambda (x) (* x 2)))

is the same as:

(setf (symbol-function 'double) (lambda (x) (* x 2)))

Why doesn't the setf in the latter return an error when it tries to evaluate
the lambda expression ? Is the #' implied ?

If that is the case, then why is:

(eq #'double (first (list #'double)))

not equivalent to:

(eq double (first (list double)))

-Luke



Sat, 02 Oct 2004 09:01:03 GMT  
 #'(lambda ... (lambda

Quote:

> I was wondering why this:

> (setf (symbol-function 'double) #'(lambda (x) (* x 2)))

> is the same as:

> (setf (symbol-function 'double) (lambda (x) (* x 2)))

#'(lambda ...) is a read-macro abbreviation for
(function (lambda ...))

(lambda ...) is specified as a macro that expands to
(function (lambda ...))

So they are simply synonyms. Choosing one over the other is purely
stylistic (now that SERIES understands normal LAMBDA forms properly :).

--
-> -/                        - Rahul Jain -                        \- <-

-> -/ "Structure is nothing if it is all you got. Skeletons spook  \- <-
-> -\  people if [they] try to walk around on their own. I really  /- <-
-> -/  wonder why XML does not." -- Erik Naggum, comp.lang.lisp    \- <-
|--|--------|--------------|----|-------------|------|---------|-----|-|
   (c)1996-2002, All rights reserved. Disclaimer available upon request.



Sat, 02 Oct 2004 10:00:33 GMT  
 #'(lambda ... (lambda

Quote:

> If that is the case, then why is:

> (eq #'double (first (list #'double)))

> not equivalent to:

> (eq double (first (list double)))

Oops, sorry, forgot to respond to this half of the question.

The "#'double" and "double" in your code are both in value positions.

The plain double looks up the variable binding of the symbol 'double
in the current scope.

The #'double expands to (function double), which looks up the function
binding of the symbol 'double in the current scope.

--
-> -/                        - Rahul Jain -                        \- <-

-> -/ "Structure is nothing if it is all you got. Skeletons spook  \- <-
-> -\  people if [they] try to walk around on their own. I really  /- <-
-> -/  wonder why XML does not." -- Erik Naggum, comp.lang.lisp    \- <-
|--|--------|--------------|----|-------------|------|---------|-----|-|
   (c)1996-2002, All rights reserved. Disclaimer available upon request.



Sat, 02 Oct 2004 10:03:14 GMT  
 #'(lambda ... (lambda

Quote:

> I was wondering why this:

> (setf (symbol-function 'double) #'(lambda (x) (* x 2)))

> is the same as:

> (setf (symbol-function 'double) (lambda (x) (* x 2)))

Because LAMBDA is a macro which expands (lambda (x) (* x 2)) into a
form equivalent to #'(lambda (x) (* x 2)) after the latter has been
read.

They're actually both notations for (function (lambda (x) (* x 2))).

#' is a reader macro which causes the form after it to be expanded
into (function THE-FORM) during the call to read.  For example "#'X"
is read as "(FUNCTION X)"; "#'(LAMBDA (x) (+ x 2))" is read as
"(FUNCTION (LAMBDA (X) (+ X 2)))".

LAMBDA is a regular macro: "(lambda (x) (+ x 2))" is read as  
"(lambda (x) (+ x 2))", which is macroexpanded into
"(function (lambda (x) (+ x 2)))".

Gabe Garza



Sat, 02 Oct 2004 10:11:58 GMT  
 #'(lambda ... (lambda
Aha, that explains a lot.  Thanks.


Quote:

> > If that is the case, then why is:

> > (eq #'double (first (list #'double)))

> > not equivalent to:

> > (eq double (first (list double)))

> Oops, sorry, forgot to respond to this half of the question.

> The "#'double" and "double" in your code are both in value positions.

> The plain double looks up the variable binding of the symbol 'double
> in the current scope.

> The #'double expands to (function double), which looks up the function
> binding of the symbol 'double in the current scope.



Sat, 02 Oct 2004 13:15:18 GMT  
 #'(lambda ... (lambda
Thank you for the reply. With that in mind, the following different example
still confuses me..

(defun our-remove-if (fn lst)
  (if (null lst)
    nil
    (if (funcall fn (first lst))
      (our-remove-if fn (rest lst))
      (cons (first lst) (our-remove-if fn (rest lst))))))

Here, (funcall fn ()) is called without a #' before the fn.

However in the following situation

(setf (symbol-function 'double) #'(lambda (x) (* x 2)))

(funcall double 100) ... fails..

(funcall #'double 100) ... works..

[You can probably tell that I'm busy working my way through "On Lisp", by
Paul Graham.]

Could you please explain why, when a function is passed as functional
argument, the #' is dropped?

-Luke


Quote:

> > I was wondering why this:

> > (setf (symbol-function 'double) #'(lambda (x) (* x 2)))

> > is the same as:

> > (setf (symbol-function 'double) (lambda (x) (* x 2)))

> Because LAMBDA is a macro which expands (lambda (x) (* x 2)) into a
> form equivalent to #'(lambda (x) (* x 2)) after the latter has been
> read.

> They're actually both notations for (function (lambda (x) (* x 2))).

> #' is a reader macro which causes the form after it to be expanded
> into (function THE-FORM) during the call to read.  For example "#'X"
> is read as "(FUNCTION X)"; "#'(LAMBDA (x) (+ x 2))" is read as
> "(FUNCTION (LAMBDA (X) (+ X 2)))".

> LAMBDA is a regular macro: "(lambda (x) (+ x 2))" is read as
> "(lambda (x) (+ x 2))", which is macroexpanded into
> "(function (lambda (x) (+ x 2)))".

> Gabe Garza



Sat, 02 Oct 2004 13:25:25 GMT  
 #'(lambda ... (lambda

Quote:
> Thank you for the reply. With that in mind, the following different example
> still confuses me..

> (defun our-remove-if (fn lst)
>   (if (null lst)
>     nil
>     (if (funcall fn (first lst))
>       (our-remove-if fn (rest lst))
>       (cons (first lst) (our-remove-if fn (rest lst))))))

> Here, (funcall fn ()) is called without a #' before the fn.

> However in the following situation

> (setf (symbol-function 'double) #'(lambda (x) (* x 2)))

> (funcall double 100) ... fails..

> (funcall #'double 100) ... works..

> [You can probably tell that I'm busy working my way through "On Lisp", by
> Paul Graham.]

> Could you please explain why, when a function is passed as functional
> argument, the #' is dropped?

It isn't.  It was never there.

The issue isn't the type of the object passed, the issue is the namespace.

Bindings in DEFUN and LAMBDA are in the 'variable' namespace.  Variables
are named by saying their name.  FN and LST in
 (DEFUN WHATEVER (FN LST) ...)
are variable bindings.  To access them, you just say FN or LST.  These might
contain any kind of object, though presumably by your choice of name you
intend the first to be a function and the second to be a list.

There is also a 'function' namespace which is specifically reserved for
functions.  That doesn't mean you can't hold functions in the variable
namespace, but you can't put numbers or hash tables in the function namespace.
The function namespace is a set of names that are accessed when you put the
name in the operator position of a form.  e.g., (car x) gets CAR from the
function namespace, not the variable namespace.

If you did
 (defun strange (list)
   (list list list list))
 (strange 3) => (3 3 3)

         ________________ These names are resolved in the variable namespace.
         vvvv vvvv vvvv
In (list list list list)
    ^^^^
  This names is resolved in the function namespace

The FUNCTION special operator allows you to grab something form the function
namespace and put it in the variable namespace.  If you want to make a list
of the list function, you can do
 (strange #'list) => (#<Function LIST> #<Function LIST> #<Function LIST>)
or you can do
 (list #'list #'list #'list)

One important reason for doing this is so you don't have to pick stupid
names list LST for your variables.  Binding a variable name LIST will not
keep the list function from being available in the body of your definition.
For example,
  (defun string-lengths (list)
    (mapcar #'(lambda (string) (list string (length string)))
            list))
  (string-lengths '("alpha" "beta"))
  => (("alpha" 5) ("beta" 4))
This is because in this:
  (defun string-lengths (list)
    (mapcar #'(lambda (string)
                (list  ; <-- This is a function reference, undisturbed
                       ;     by your having bound LIST above
                  string
                  (length string)))
            list)) ;<-- This is a variable ref to the function argument.



Sat, 02 Oct 2004 13:47:18 GMT  
 #'(lambda ... (lambda

Quote:
> Could you please explain why, when a function is passed as functional
> argument, the #' is dropped?

The functional argument is passed to the function and bound to the
_variable_ value of some identifier. Therefore, you don't want the
function binding of that identifier; rather, you want the (normal)
value of it.

--
-> -/                        - Rahul Jain -                        \- <-

-> -/ "Structure is nothing if it is all you got. Skeletons spook  \- <-
-> -\  people if [they] try to walk around on their own. I really  /- <-
-> -/  wonder why XML does not." -- Erik Naggum, comp.lang.lisp    \- <-
|--|--------|--------------|----|-------------|------|---------|-----|-|
   (c)1996-2002, All rights reserved. Disclaimer available upon request.



Sat, 02 Oct 2004 13:46:05 GMT  
 #'(lambda ... (lambda
[ Luke J Crook ]

Quote:
> [You can probably tell that I'm busy working my way through "On Lisp", by
> Paul Graham.]

I wonder, is this your first Lisp book? As you seem to struggle with
such basic, yet possibly strange to non-Lispers, concepts as symbols,
maybe you'd be better off with reading a more introductory book first,
e.g. Graham's ANSI Common Lisp (or at least parts of such a book). On
Lisp is not quite an introductory book, but rather a fine continuation
of e.g. ANSI Common Lisp.

Martin
--
"An ideal world is left as an exercise to the reader."
                                                 -Paul Graham, On Lisp



Sat, 02 Oct 2004 16:28:52 GMT  
 #'(lambda ... (lambda
* "Luke Crook"
| I was wondering why this:
|
| (setf (symbol-function 'double) #'(lambda (x) (* x 2)))
|
| is the same as:
|
| (setf (symbol-function 'double) (lambda (x) (* x 2)))

  They are not the same.  They have the same effect.  Lots of things have
  the same effect.  Just because two things have the same effect, does not
  mean you can use the difference as basis for any inference about other
  things that are similarly different without understanding the cause.
  Quite elementary logic, this.  If you do not command a reasonable level
  of reasoning, you will go wrong in so many wasteful ways that you should
  go back to study reasoning and logic, and not attempt to stumble along
  with random guesswork and clueless trial and error.

| Why doesn't the setf in the latter return an error when it tries to
| evaluate the lambda expression?

  Look it up in your favorite Common Lisp reference.  Things like this are
  very well explained in every serious introduction to Common Lisp, and
  then you have the specification.

  But how did you manage to write this code in the first place?  It is so
  odd to mess with the symbol-function slot of a symbol without knowing
  what #' means or how the macro lambda works.

///
--
  In a fight against something, the fight has value, victory has none.
  In a fight for something, the fight is a loss, victory merely relief.

  Post with compassion: http://home.chello.no/~xyzzy/kitten.jpg



Sun, 03 Oct 2004 02:37:28 GMT  
 #'(lambda ... (lambda

Quote:
> [ Luke J Crook ]

> > [You can probably tell that I'm busy working my way through "On Lisp",
by
> > Paul Graham.]

> I wonder, is this your first Lisp book?

First book, yes. However I have been working a few other of resources on the
net, http://psg.com/~dlamkins/sl/contents.html being one example. I
downloaded On Lisp as it was available online.

Quote:
> As you seem to struggle with
> such basic, yet possibly strange to non-Lispers, concepts as symbols,

Lisp is quite different to anything I have learnt. And pretty much
everything about Lisp is strange to non-Lispers.

I think if programming languages were to be compared to preparing a meal,
then Lisp would be the old Italian chef who makes his pasta from scratch.
Other languages seem more like the student who throws a TV dinner into the
microwave. The chef might be slower but his guests will have a lot less
indigestion after the fact.

Quote:
> On
> Lisp is not quite an introductory book, but rather a fine continuation
> of e.g. ANSI Common Lisp.

Thanks for the pointer. I will definitely make that my next purchase.

-Luke



Sun, 03 Oct 2004 15:12:24 GMT  
 #'(lambda ... (lambda

Quote:
> I think if programming languages were to be compared to preparing a meal,
> then Lisp would be the old Italian chef who makes his pasta from scratch.
> Other languages seem more like the student who throws a TV dinner into the
> microwave. The chef might be slower but his guests will have a lot less
> indigestion after the fact.

Actually, Lisp tends to be very fast to use, once you get the hang of
it.  Even if I'm not planning on writing something in CL, I've been
known to play around with an initial prototype in CL because it's so
fast to put together.  Then I have more chance to play with ideas.  To
continue the cooking metaphor, I'd say Lisp is more like a balloon
whisk -- it might appear more difficult to use for someone used to an
electric gizmo, but it will beat those egg whites up faster and
higher, once you learn to use it right.

--
           /|_     .-----------------------.                        
         ,'  .\  / | No to Imperialist war |                        
     ,--'    _,'   | Wage class war!       |                        
    /       /      `-----------------------'                        
   (   -.  |                              
   |     ) |                              
  (`-.  '--.)                              
   `. )----'                              



Sun, 03 Oct 2004 15:32:09 GMT  
 
 [ 22 post ]  Go to page: [1] [2]

 Relevant Pages 

1. (lambda ()) vs #'(lambda ())

2. no re-binding of nested-scope locals, no re-binding in lambda, no,,, {was Re: lambda)

3. Of what use is 'lambda'???

4. Of what use is 'lambda'???

5. I really don't understand Lambda!

6. Let's Talk About Lambda Functions!

7. Beginners' Question -- Assigning variables in a lambda

8. Generalized slicing and a question on lambdas (lambda's)

9. (compiled-function-p #'(lambda (x) (+ x x)))

10. (satisfies #'(lambda (x) t))

11. Basics of #'(lambda ...

12. Quoting disassemble's lambda argument.

 

 
Powered by phpBB® Forum Software