From: ···············@gmail.com
Subject: Questions regarding functions and #'
Date: 
Message-ID: <1171610941.008749.122630@j27g2000cwj.googlegroups.com>
Hi All,
my roommate and I are studying Lisp and Scheme, and we have a bunch of
questions regarding the use of #'.

In Scheme we can do:

(define (foo x)
  (x))

(define (bar)
  4)

> (foo bar)
returns 4 as a response.



Instead, in Lisp if we type:
(defun foo (x)
  (x))

(defun bar ()
  4)

> (foo bar)
returns the following error:
Attempt to take the value of the unbound variable `BAR'.
   [Condition of type UNBOUND-VARIABLE]

And also
> (foo 'bar)
returns this other error:
attempt to call `X' which is an undefined function.
   [Condition of type UNDEFINED-FUNCTION]



We verified that in Lisp the following do the right thing:
(defun foo (x)
  (funcall x))

(defun bar ()
  4)

> (foo #'bar)
returns 4

Or

> (foo 'bar)
returns 4



Question #1: why in Lisp do we have to use funcall and cannot just
pass the symbol bar and use the parens inside foo to get it executed?

Question #2: if now we do: (setf baz #'bar), baz is a symbol bound to
the function object to which bar is bound as well. However, if we
evaluate them:
> baz
#<Function BAR>

whereas

> bar
returns Attempt to take the value of the unbound variable `BAR'.
   [Condition of type UNBOUND-VARIABLE

What is the difference between bar and baz ?

Question #3: we know that in Lisp a symbol can be bound to both a
function and a variable at the same time, while in Scheme one binding
would exclude the other. Is this difference that make #' necessary in
Lisp ? Why did the language designers of Lisp and Scheme choose one
option over the other and what are their implications?

Please answer the questions or point us to documents discussing these
particular issues.

thanks a lot!

happy (OR lisping scheming)

Chris & Enrico

From: Pascal Costanza
Subject: Re: Questions regarding functions and #'
Date: 
Message-ID: <53l51jF1snac0U1@mid.individual.net>
···············@gmail.com wrote:

> Please answer the questions or point us to documents discussing these
> particular issues.

Yes, sir! See http://www.nhplace.com/kent/Papers/Technical-Issues.html

Also google for "Lisp-2" and "Lisp-1".


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Rainer Joswig
Subject: Re: Questions regarding functions and #'
Date: 
Message-ID: <joswig-40880A.09511216022007@news-europe.giganews.com>
In article <························@j27g2000cwj.googlegroups.com>,
 ···············@gmail.com wrote:

> Hi All,
> my roommate and I are studying Lisp and Scheme, and we have a bunch of
> questions regarding the use of #'.
> 
> In Scheme we can do:
> 
> (define (foo x)
>   (x))
> 
> (define (bar)
>   4)
> 
> > (foo bar)
> returns 4 as a response.
> 
> 
> 
> Instead, in Lisp if we type:
> (defun foo (x)
>   (x))
> 
> (defun bar ()
>   4)
> 
> > (foo bar)
> returns the following error:
> Attempt to take the value of the unbound variable `BAR'.
>    [Condition of type UNBOUND-VARIABLE]
> 
> And also
> > (foo 'bar)
> returns this other error:
> attempt to call `X' which is an undefined function.
>    [Condition of type UNDEFINED-FUNCTION]
> 
> 
> 
> We verified that in Lisp the following do the right thing:
> (defun foo (x)
>   (funcall x))
> 
> (defun bar ()
>   4)
> 
> > (foo #'bar)
> returns 4
> 
> Or
> 
> > (foo 'bar)
> returns 4

Yep.

> 
> 
> 
> Question #1: why in Lisp do we have to use funcall and cannot just
> pass the symbol bar and use the parens inside foo to get it executed?

Because in Common Lisp the first position in an S-expression is not
evaluated. It is defined this way. You can look it up in the
ANSI CL Spec (via the HyperSpec for example).

> 
> Question #2: if now we do: (setf baz #'bar), baz is a symbol bound to
> the function object to which bar is bound as well. However, if we
> evaluate them:
> > baz
> #<Function BAR>
> 
> whereas
> 
> > bar
> returns Attempt to take the value of the unbound variable `BAR'.
>    [Condition of type UNBOUND-VARIABLE
> 
> What is the difference between bar and baz ?

Symbols in Common Lisp have value and function cells.

BAR has the function cell bound to a function.
BAZ now has the value cell bound to a function.

(setf baz #'bar) is similar to (setf (symbol-value 'baz) #'bar)

What you may want is (setf (symbol-function 'baz) #'bar)

Now you can use (baz ...)


> 
> Question #3: we know that in Lisp a symbol can be bound to both a
> function and a variable at the same time,

Since in Common Lisp, a symbol has both a value and a function cell.

> while in Scheme one binding
> would exclude the other. Is this difference that make #' necessary in
> Lisp ? Why did the language designers of Lisp and Scheme choose one
> option over the other and what are their implications?

Pascal already gave you a link to a good explanation.

> 
> Please answer the questions or point us to documents discussing these
> particular issues.
> 
> thanks a lot!
> 
> happy (OR lisping scheming)
> 
> Chris & Enrico
From: Pascal Bourguignon
Subject: Re: Questions regarding functions and #'
Date: 
Message-ID: <874ppmfiia.fsf@thalassa.informatimago.com>
···············@gmail.com writes:

> Question #3: we know that in Lisp a symbol can be bound to both a
> function and a variable at the same time, while in Scheme one binding
> would exclude the other. Is this difference that make #' necessary in
> Lisp ? 

Yes.

> Why did the language designers of Lisp and Scheme choose one
> option over the other and what are their implications?

In the origin (eg LISP 1.5 and before), symbols were actually
implemented just as their property list, and their name and value
where properties in this plist.  So it was natural to have one
property for a function binding (actually different properties
according to the kind of function, built-in, etc)  and another for a
value binding.

Since distinguishing a value slot from a function slot leads to some
interesting properties, similar to what we are used to in natural
languages, it has been kept over the years.

              The fly flies. The flies fly.

The same word can be verb or noun.

(defun flies (subject)
   (assert (singular subject))
   ...)

(defun fly (subjects)
   (assert (plural subjects))
   ...)


(let ((fly (make-instance 'fly :name "Jojo"))
      (flies (list (make-instance 'fly :name "Bobo")
                   (make-instance 'fly :name "Popo"))))
   (fly   flies)
   (flies fly))


On the other hand, in scheme they took the opportunity of defining a
clean and simplier language from the start, so they have only one
thing bound to a name at a time.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
From: Rob Thorpe
Subject: Re: Questions regarding functions and #'
Date: 
Message-ID: <1171639826.883506.103870@l53g2000cwa.googlegroups.com>
> Since distinguishing a value slot from a function slot leads to some
> interesting properties, similar to what we are used to in natural
> languages, it has been kept over the years.
>
>               The fly flies. The flies fly.
>
> The same word can be verb or noun.

But, some human languages probably go too far.

As a great man said "Time flies like and arrow, fruit flies like a
banana".
From: Vassil Nikolov
Subject: Re: Questions regarding functions and #'
Date: 
Message-ID: <yy8vps89grwy.fsf@eskimo.com>
On 15 Feb 2007 23:29:01 -0800, ···············@gmail.com said:

| ...
| Question #1: why in Lisp do we have to use funcall and cannot just
| pass the symbol bar and use the parens inside foo to get it executed?

  The answer is contained in question #3 (so see also below).  Another
  answer is that that is the way Common Lisp and Scheme are...

| Question #2: if now we do: (setf baz #'bar), baz is a symbol bound to
| the function object to which bar is bound as well. However, if we
| evaluate them:
|| baz
| #<Function BAR>

| whereas

|| bar
| returns Attempt to take the value of the unbound variable `BAR'.
|    [Condition of type UNBOUND-VARIABLE

| What is the difference between bar and baz ?

        variable slot   function slot
        -------------   -------------
  BAR   unbound         holds #'BAR
  BAZ   holds #'BAR     unbound

  See also BOUNDP, FBOUNDP, SYMBOL-FUNCTION, SYMBOL-VALUE.

| Question #3: we know that in Lisp a symbol can be bound to both a
| function and a variable at the same time, while in Scheme one binding
| would exclude the other. Is this difference that make #' necessary in
| Lisp ?

  Yes.

| Why did the language designers of Lisp and Scheme choose one
| option over the other and what are their implications?

  (Note: this is somewhat of a contentious issue...)

  Common Lisp is closer in this respect to the original LISP as it came
  into being in the late 50s, when it seems this way of defining the
  evaluation rules (a symbol in the first position of a form is
  evaluated differently from symbols as atoms) was the one that came
  naturally.

  Scheme, on the other hand, was designed to be "purer" and is closer to
  the original lambda-calculus, which has only one type of binding for
  each variable.

  Both approaches have their advantages.  For example, in Common Lisp it
  is not necessary to call a variable that holds a list "lst" (to avoid
  clobbering the list function), while higher-order programming in
  Scheme avoids the "APPLY/FUNCALL clutter".

| Please answer the questions or point us to documents discussing these
| particular issues.

  Also, in search for further reading, use Lisp-1 and Lisp-2 as search
  terms (Scheme is a Lisp-1, Common Lisp is a Lisp-2 and so is Elisp).

  ---Vassil.

-- 
Our programs do not have bugs; it is just that the users' expectations
differ from the way they are implemented.
-- 

-- 
Vassil Nikolov <········@pobox.com>
The quick brown fix jumped over the lazy bugs.