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

Why does (cons + (0 2)) equal (<#procedure +> (0 2)) in Pocket Scheme? Shouldn't it equal 2?

1 view
Skip to first unread message

Jonathan Mark

unread,
Mar 11, 2008, 11:11:37 PM3/11/08
to
Why does (cons + '(0 2)) equal (#<procedure +> 0 2) in Pocket Scheme?
Shouldn't it equal 2?

How can I modify the expression (cons + '(0 2)) so that it equals
2?

Barry Margolin

unread,
Mar 11, 2008, 11:26:50 PM3/11/08
to
In article
<b35ac77b-beeb-4c3d...@e39g2000hsf.googlegroups.com>,
Jonathan Mark <jonatha...@yahoo.com> wrote:

> Why does (cons + '(0 2)) equal (#<procedure +> 0 2) in Pocket Scheme?
> Shouldn't it equal 2?

No. The cons function returns a pair, not a number.

> How can I modify the expression (cons + '(0 2)) so that it equals
> 2?

(apply + '(0 2))

or perhaps you're looking for:

(eval (cons '+ '(0 2)))

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***

Jonathan Mark

unread,
Mar 12, 2008, 2:56:00 AM3/12/08
to
>>> No. The cons function returns a pair, not a number.<<<

Thanks for suggesting eval. I will use it. However, I am missing
something here.

It seems to me that (#<procedure +> 0 2) is the same as (+ 0 2) which
is a pair whose car is + and whose cdr is (0 2) . And the pair (+ 0 2)
is 2.

Ray Dillinger

unread,
Mar 12, 2008, 4:33:31 AM3/12/08
to
Jonathan Mark wrote:

No, + is a symbol (or a variable name). #<procedure +> is a procedure
that adds numbers (and it's the value of the variable named +). When
you say (cons + '(0 2)) you're asking for a pair that has the value of
+ in the car and the value of (quote (0 2)), which is the list (0 2),
in the cdr.

And that's what you got. At no time did you do anything that would have
actually _called_ #<procedure +>. You got the _value_ of the variable
+, and you put it into a list, but you didn't actually call (or apply)
that value.

Bear

Max Hailperin

unread,
Mar 12, 2008, 8:23:22 AM3/12/08
to
Jonathan Mark <jonatha...@yahoo.com> writes:

>... However, I am missing


> something here.
>
> It seems to me that (#<procedure +> 0 2) is the same as (+ 0 2) which
> is a pair whose car is + and whose cdr is (0 2) . And the pair (+ 0 2)
> is 2.

Although there are several technical points regarding Scheme that I
could make here (most of which Bear already made in his reply), I
think the broader issue is a philosophical one about the difference
between a symbol for an object and the object being symbolized. In
Scheme, and computer science more generally, we are very careful about
this distinction in a variety of contexts, such as distinguishing
names from what they name and distinguishing expressions from their
values. However, quite a bit on this topic was publibshed even by
philosphers who predated computer science and have never encountered
Scheme. See, for example, the summary of Frege's struggles with this
topic at http://www.iep.utm.edu/f/frege.htm#H4

-max (as name and object: http://gustavus.edu/+max/max.jpg)

Pascal J. Bourguignon

unread,
Mar 12, 2008, 9:48:56 AM3/12/08
to
Jonathan Mark <jonatha...@yahoo.com> writes:

One problem in your question is the meaning of "equal".

Note that (eqv? (eval (cons + '(0 2))) 2) returns #t,
but (equal? (cons + '(0 2)) (cons '+ '(0 2))) returns #f.


If you define your "equal" as:
(define (equale? a b) (equal? (eval a) (eval b)))

then:
(equale? (cons + '(0 2)) (list '/ 4 2))
and
(equale? (cons + '(0 2)) 2)
will return #t.

Have a look at: http://www.nhplace.com/kent/PS/EQUAL.html


Once you understand what you mean by "equal", you'll be able to build
the expressions you really mean to build.

--
__Pascal Bourguignon__

Jonathan Mark

unread,
Mar 12, 2008, 12:42:25 PM3/12/08
to
Thanks for the responses, and in particular the reference to Frege.

My understanding is now as follows:

My name is Jonathan, and Jonathan is a person, but the name Jonathan
is not a person.

+ is the name of #<procedure +>.

When I say (cons + '(0 2)) the cons operation examines the name + and
places the object referred to by that name #<procedure +> at the front
of a list. It does not actually apply the procedure unless I instruct
it to using eval or apply.

(+ 0 2) on the other hand, begins not with a procedure but with a
symbol for a procedure. REPL should apply the procedure represented by
+ to the rest of the list.

REPL does not apply procedures found at the front of lists. Rather, it
only applies names of procedures in the front of lists.

Barry Margolin

unread,
Mar 12, 2008, 8:10:06 PM3/12/08
to
In article
<acb88ff5-0d12-4ed7...@a70g2000hsh.googlegroups.com>,
Jonathan Mark <jonatha...@yahoo.com> wrote:

> Thanks for the responses, and in particular the reference to Frege.
>
> My understanding is now as follows:
>
> My name is Jonathan, and Jonathan is a person, but the name Jonathan
> is not a person.
>
> + is the name of #<procedure +>.
>
> When I say (cons + '(0 2)) the cons operation examines the name + and

Not quite. Parameters to functions are evaluated automatically, and the
result of evaluating the expressions are passed to the cons procedure.

> places the object referred to by that name #<procedure +> at the front
> of a list. It does not actually apply the procedure unless I instruct
> it to using eval or apply.

That part is correct. All that cons does is construct a pair. What
happens with that pair depends on what you do with the result of cons.
If you're just typing to the REPL, all it does is print the result.

> (+ 0 2) on the other hand, begins not with a procedure but with a
> symbol for a procedure. REPL should apply the procedure represented by
> + to the rest of the list.
>
> REPL does not apply procedures found at the front of lists. Rather, it
> only applies names of procedures in the front of lists.

Remember what REPL stands for: Read-Eval-Print. So it reads an
expression, evaluates it, and prints the result. It DOESN'T evaluate
the result. If it did, you would expect an error if you typed:

(list 1 2 3)

because this returns the list (1 2 3), which doesn't make sense to
evaluate.

Here's another way to think of it:

(define f 1)

(cons f '(0 2)) ; #1
(cons + '(0 2)) ; #2

Is there any syntactic or semantic difference between #1 and #2? f and
+ are both just names. One happens to refer to a number, while the
other refers to a procedure, but that shouldn't matter.

0 new messages