Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A example in SICP

1 view
Skip to first unread message

Yongwei Xing

unread,
Jan 3, 2010, 1:40:34 AM1/3/10
to
Hi all

Today I read the book SICP, chapter 3. I read the code below
(define (make-simplified-withdraw balance)
(lambda (amount)
(set! balance (- balance amount))
balance))

I use the common lisp write like below
(defun (make-simplified-withdraw balance)
(lambda (amount)
(setf balance (- balance amount))
balance))

Then I use it like below:
(defvar x (make-simplified-withdraw 100) )
(x 10)
It would give a error:
Then I use it
(funcall x 10)
It works.
Can someone explain why?

Best Regards,

D Herring

unread,
Jan 3, 2010, 1:59:04 AM1/3/10
to
Yongwei Xing wrote:
> Hi all
>
> Today I read the book SICP, chapter 3. I read the code below
> (define (make-simplified-withdraw balance)
> (lambda (amount)
> (set! balance (- balance amount))
> balance))
>
> I use the common lisp write like below
> (defun (make-simplified-withdraw balance)
> (lambda (amount)
> (setf balance (- balance amount))
> balance))

You mean
(defun make-simplified-withdraw (balance)


(lambda (amount)
(setf balance (- balance amount))
balance))

?

> Then I use it like below:
> (defvar x (make-simplified-withdraw 100) )
> (x 10)
> It would give a error:
> Then I use it
> (funcall x 10)
> It works.
> Can someone explain why?

Scheme is a "lisp-1" and Common Lisp (CL) is a "lisp-2". FYI, the
terms were coined in the following paper which you don't need to read
right now.
http://www.nhplace.com/kent/Papers/Technical-Issues.html

The essential point is that CL symbols have two values: a
"symbol-function" and a "symbol-value". Scheme symbols only have a
single value. CL uses the symbol-function for the first entry in a
form, and the symbol-value for the rest. Scheme uses the same value
in both places.

Consider the simple example (f f).
In common lisp, this represents a function named f being passed the
value in variable f (which is probably not the function f).
In scheme, this represents the function f being passed itself.

A CL defvar sets the symbol-value, not the symbol-function. So when
you evaluate (x 10), CL complains that the symbol-function was not
set. When you (funcall x 10), funcall receives the value of x (a
lambda function) and the argument 10.

Does that help?

- Daniel

Yongwei Xing

unread,
Jan 3, 2010, 2:06:05 AM1/3/10
to
> right now.http://www.nhplace.com/kent/Papers/Technical-Issues.html

>
> The essential point is that CL symbols have two values: a
> "symbol-function" and a "symbol-value". Scheme symbols only have a
> single value. CL uses the symbol-function for the first entry in a
> form, and the symbol-value for the rest. Scheme uses the same value
> in both places.
>
> Consider the simple example (f f).
> In common lisp, this represents a function named f being passed the
> value in variable f (which is probably not the function f).
> In scheme, this represents the function f being passed itself.
>
> A CL defvar sets the symbol-value, not the symbol-function. So when
> you evaluate (x 10), CL complains that the symbol-function was not
> set. When you (funcall x 10), funcall receives the value of x (a
> lambda function) and the argument 10.
>
> Does that help?
>
> - Daniel

Hi Daniel

Thanks very much for your detailed explanation.

One question, if I want to use it like the Schema (f 10). What should
I do in the commin lisp?

Best Regards,

D Herring

unread,
Jan 3, 2010, 2:20:01 AM1/3/10
to
Yongwei Xing wrote:

> On 1锟斤拷3锟斤拷, 锟斤拷锟斤拷2时59锟斤拷, D Herring <dherr...@at.tentpost.dot.com> wrote:
>> Yongwei Xing wrote:
>>> Today I read the book SICP, chapter 3. I read the code below
>>> (define (make-simplified-withdraw balance)
>>> (lambda (amount)
>>> (set! balance (- balance amount))
>>> balance))
>>
>> (defun make-simplified-withdraw (balance)
>> (lambda (amount)
>> (setf balance (- balance amount))
>> balance))

> Thanks very much for your detailed explanation.
You're welcome.

> One question, if I want to use it like the Schema (f 10). What should
> I do in the commin lisp?

(defvar x) ; you can give it a value if desired
(setf (symbol-function 'x) (make-simplified-withdraw 10))
(x 10)

Later,
Daniel

Nicolas Neuss

unread,
Jan 3, 2010, 7:06:28 AM1/3/10
to
Yongwei Xing <jdxy...@gmail.com> writes:

> Thanks very much for your detailed explanation.
>
> One question, if I want to use it like the Schema (f 10). What should
> I do in the commin lisp?

You mean "The Coming Lisp"? :-)

Ask yourself also the question if you really that short notation. The
interpretation of this message passing OO stuff is something like "send
object message", so instead of getting around funcall one better should
rename it:

(defun send (object arg)
(funcall object arg))

(I have introduced such a send function even when using Scheme.)

Nicolas

Nicolas Neuss

unread,
Jan 3, 2010, 7:14:20 AM1/3/10
to
Nicolas Neuss <last...@math.uni-karlsruhe.de> writes:

> Ask yourself also the question if you really that short notation. The

^
want


> interpretation of this message passing OO stuff is something like "send
> object message", so instead of getting around funcall one better should
> rename it:
>
> (defun send (object arg)
> (funcall object arg))
>
> (I have introduced such a send function even when using Scheme.)

Or better

(defun send (object method &rest args)
(apply (funcall object method) args))

which can be used without many parens as

(send balance 'withdraw 10)

Nicolas

Pascal J. Bourguignon

unread,
Jan 3, 2010, 9:37:25 AM1/3/10
to
D Herring <dher...@at.tentpost.dot.com> writes:

> Yongwei Xing wrote:


>> On 1月3日, 下午2时59分, D Herring <dherr...@at.tentpost.dot.com> wrote:
>>> Yongwei Xing wrote:
>>>> Today I read the book SICP, chapter 3. I read the code below
>>>> (define (make-simplified-withdraw balance)
>>>> (lambda (amount)
>>>> (set! balance (- balance amount))
>>>> balance))
>>>
>>> (defun make-simplified-withdraw (balance)
>>> (lambda (amount)
>>> (setf balance (- balance amount))
>>> balance))
>
>> Thanks very much for your detailed explanation.
> You're welcome.
>
>> One question, if I want to use it like the Schema (f 10). What should
>> I do in the commin lisp?
> (defvar x) ; you can give it a value if desired

There's no need to declare a special variable if you don't need it.

Just the following will do:

> (setf (symbol-function 'x) (make-simplified-withdraw 10))
> (x 10)
>
> Later,
> Daniel

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

0 new messages