From: David Sletten
Subject: Re: First element of a quoted list
Date: 
Message-ID: <VXRkd.82230$Kl3.41251@twister.socal.rr.com>
gewoon.iemand wrote:

> Hi
> 
> I'm implementing a LISP interpreter in JAVA. I have made a structure
> of classes to express, parse and evalute LISP elements. There is an
> SExpression base class and, among others, a Symbol and a Functor sub
> class.
> 
> When I parse a list, the parser expects the first element to be a
> functor (I deal with operators in the same way as I deal with
> functions). When the list is evaluated, it evaluates every element and
> then feeds it to the functor, which searches for the function to use
> (dynamic), and executes.
> 

Your chief problem seems to be that you don't yet understand the basics 
of Lisp sufficiently to be attempting this project. This is evident in 
your statement above that you deal with operators in the same way as you 
deal with functions. Presumably you have drawn a distinction in your 
mental model of Lisp between 'operators' such as + and 'functions' which 
may be defined by the user? This is not how Lisp works. If you are not 
familiar with the CLHS, then take a look here:
http://www.lispworks.com/reference/HyperSpec/Body/03_abab.htm

As you can see, Lisp operators are either special operators, macros, or 
functions. Functions follow the evaluation rule you are implementing:
1. Evaluate arguments from left to right.
2. Apply the function to the values determined above.
On the other hand, both special operators and macros do not follow this 
basic rule. Not all of the arguments will be evaluated. This is 
important, for example, in Lisp control structures:
(if (>= x y) (write-line "X is max.") (write-line "Y is max."))
If the operator IF were a function, then both of the WRITE-LINE forms 
would be evaluated above. Of course we want _conditional_ evaluation of 
these forms--that's the whole point.


> So, when I enter (+ 1 2 3) i get 6. When I enter (+ a b c) I get a
> number, because the + function gets the value of the symbols a b and c
> when they get evaluated (assuming they're bound to a value).
> 
> However, I'm running into a problem when I parse quoted lists. I could
> enter '(aVariable 1 2 3) or '(aFunction 1 2 3). When it gets parsed,
> the parser cannot know whether its a variable or a functor. I could
> also enter '(1 2 3 4), and the first element is neither.
> 
> How should I deal with this? Is my implementation wrong and should I
> make the determination of whether to treat the first element as a
> symbol, functor or constant at run time, when a list is evaluated?

As you can see from the link above, QUOTE is a special operator in Lisp. 
You should be aware that '(a b c) is simply a syntactic shortcut for 
(quote a b c). Thus, '(a b c) is not 'evaluated' by the function rule. 
In fact, all QUOTE does is return the quoted object _without_ evaluation:
'(a b c) => (A B C)
'(+ 2 3) => (+ 2 3)
So, yes your implementation is wrong and you should not be treating the 
first element as anything special--the list is _not_ evaluated.

> 
> Also, the setq function seems illogical. Suppose I make a function for
> setq. Then the elements get evaluated and they are fed to the setq
> function. But in the case of the example (setq a 1), a is evaluated
> and becomes a value (or throws an error if it's not bound). The setq
> function does not need a value, it needs the variable! I tought of
> making the parser translate setq into set ' , but that feels wrong.
> 

What do you think SETQ stands for? "set quote". SETQ is also a special 
operator. Its first argument should be a symbol, which is not evaluated. 
So your examples _are_ identical: (setq a 1) vs. (set 'a 1)

> This leads me to the idea that evaluating before the function gets
> executed is wrong. My implementation does not have a problem with set.
> When (set 'a 1) gets evaluated, 'a evaluates into a, which is what the
> set function needs to know in order to do what it's supposed to do
> (namely binding "a" to the value 1). How do other implementations deal
> with this?

The evaluation rule for functions that Lisp uses is known as 
'applicative order'--arguments are evaluated before function 
application. Some other languages use a different rule known as 'normal 
order', where arguments are not evaluated until actually needed. If you 
want to implement Lisp, then evaluating arguments before calling a 
function is not wrong--it's necessary. Of course, you may experiment 
with other ideas, but then you're not implementing Lisp.

David Sletten

From: Duane Rettig
Subject: Re: First element of a quoted list
Date: 
Message-ID: <4actn6ep7.fsf@franz.com>
David Sletten <·····@slytobias.com> writes:

> gewoon.iemand wrote:

> > Also, the setq function seems illogical. Suppose I make a function
> > for
> 
> > setq. Then the elements get evaluated and they are fed to the setq
> > function. But in the case of the example (setq a 1), a is evaluated
> > and becomes a value (or throws an error if it's not bound). The setq
> > function does not need a value, it needs the variable! I tought of
> > making the parser translate setq into set ' , but that feels wrong.
> >
> 
> 
> What do you think SETQ stands for? "set quote". SETQ is also a special
> operator. Its first argument should be a symbol, which is not
> evaluated. So your examples _are_ identical: (setq a 1) vs. (set 'a 1)

Well, sort of.  SETQ recognizes lexical variables.  SET does not

YMMV:

CL-USER(1): (defun foo (a) (setq a 10) (set 'a 20) a)
FOO
CL-USER(2): (foo 30)
10
CL-USER(3): a
20
CL-USER(4): 

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: David Sletten
Subject: Re: First element of a quoted list
Date: 
Message-ID: <KnUkd.41526$jo2.3329@twister.socal.rr.com>
Duane Rettig wrote:


>>So your examples _are_ identical: (setq a 1) vs. (set 'a 1)
> 
> 
> Well, sort of.  SETQ recognizes lexical variables.  SET does not
> 
> YMMV:
> 
> CL-USER(1): (defun foo (a) (setq a 10) (set 'a 20) a)
> FOO
> CL-USER(2): (foo 30)
> 10
> CL-USER(3): a
> 20
> CL-USER(4): 
> 
Yeah, I was leaning towards choosing a different word than 'identical'.

Thanks for keeping me straight.

David Sletten
From: David Sletten
Subject: Re: First element of a quoted list
Date: 
Message-ID: <uJSkd.41440$jo2.23156@twister.socal.rr.com>
> As you can see from the link above, QUOTE is a special operator in Lisp. 
> You should be aware that '(a b c) is simply a syntactic shortcut for 
> (quote a b c). 
Oops. I made the same mistake Geoff pointed out. That should be (quote 
(a b c)), of course.

David Sletten