From: ······@ieee.org
Subject: newbie Q: opposite of quote
Date: 
Message-ID: <1161781127.333309.205350@e3g2000cwe.googlegroups.com>
Hi:

Is it fair to say that eval is the opposite of quote?

(setf a 3)
--> 3
(eval (quote a))
--> 3

Is there another function considered the opposite of quote?

Adam

From: dpapathanasiou
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <1161783015.905051.95970@h48g2000cwc.googlegroups.com>
It's better to think of it this way: anything not quoted *will* be
evaluated as an expression and return a value.
From: ······@ieee.org
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <1161787433.448943.220340@i3g2000cwc.googlegroups.com>
dpapathanasiou wrote:
> It's better to think of it this way: anything not quoted *will* be
> evaluated as an expression and return a value.

My question is really: how do you unquote something. Eval is what I've
found so far.

Adam
From: Bill Atkins
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <m2lkn48jvn.fsf@bertrand.local>
······@ieee.org writes:

> dpapathanasiou wrote:
>> It's better to think of it this way: anything not quoted *will* be
>> evaluated as an expression and return a value.
>
> My question is really: how do you unquote something. Eval is what I've
> found so far.
>
> Adam

Unquote?  What do you mean by that?
From: Pascal Bourguignon
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <87hcxstf9d.fsf@thalassa.informatimago.com>
······@ieee.org writes:

> dpapathanasiou wrote:
>> It's better to think of it this way: anything not quoted *will* be
>> evaluated as an expression and return a value.
>
> My question is really: how do you unquote something. Eval is what I've
> found so far.

(defun unquote (something)
  (if (and (consp something) (eq 'quote (first something)))
     (second something)
     something))

(unquote '(quote (quote a))) --> (QUOTE A)  ; may be displayed as 'A 
                                            ; by your lisp printer
(unquote '(quote a))         --> A
(unquote 'a)                 --> A


See how much easier it's when you ask your real question?

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

CONSUMER NOTICE: Because of the "uncertainty principle," it is
impossible for the consumer to simultaneously know both the precise
location and velocity of this product.
From: Pascal Bourguignon
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <87vem8rytm.fsf@thalassa.informatimago.com>
Pascal Bourguignon <···@informatimago.com> writes:

> ······@ieee.org writes:
>
>> dpapathanasiou wrote:
>>> It's better to think of it this way: anything not quoted *will* be
>>> evaluated as an expression and return a value.
>>
>> My question is really: how do you unquote something. Eval is what I've
>> found so far.
>
> (defun unquote (something)
>   (if (and (consp something) (eq 'quote (first something)))
>      (second something)
>      something))
>
> (unquote '(quote (quote a))) --> (QUOTE A)  ; may be displayed as 'A 
>                                             ; by your lisp printer
> (unquote '(quote a))         --> A
> (unquote 'a)                 --> A

I should add: remember that unquote here is a function, so you have to
quote any literal argument you give it.  Not so when the  argument is
stored in a variable:

(let ((x (read-from-string "'A")))
  (format t "~&(~{~S~^ ~})~%" x)
  (unquote x))

prints:  (QUOTE A)
returns: A

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

"I have challenged the entire quality assurance team to a Bat-Leth
contest.  They will not concern us again."
From: Marcin 'Qrczak' Kowalczyk
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <87bqo0qjtc.fsf@qrnik.zagroda>
······@ieee.org writes:

> My question is really: how do you unquote something.
> Eval is what I've found so far.

What do you mean by unquoting?

The _form_ (+ 2 2) evaluates to the _value_ 4.

The _form_ (quote (+ 2 2)) evaluates to the _value_ (+ 2 2).

The function eval, when applied to the _value_ (+ 2 2), returns the
_value_ 4. This can be seen by evaluating the _form_ (eval (quote (+ 2 2)),
which returns the _value_ (+ 2 2).

Note that forms and values are different concepts. While the _form_
(eval (quote (+ 2 2)) evaluates to the _value_ (+ 2 2), the _form_
(+ 2 2) does not evaluate to the _value_ (+ 2 2) but to 4, so we can't
say that eval and quote cancel out each other.

When eval is applied to the _value_ (* 2 2), it also returns the
_value_ 4.

There is no inverse of eval. What would it return for 4: (+ 2 2),
(* 2 2), or something else?

-- 
   __("<         Marcin Kowalczyk
   \__/       ······@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/
From: Jules
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <1161794247.434543.40690@i3g2000cwc.googlegroups.com>
······@ieee.org wrote:
> Hi:
>
> Is it fair to say that eval is the opposite of quote?
>
> (setf a 3)
> --> 3
> (eval (quote a))
> --> 3
>
> Is there another function considered the opposite of quote?
>
> Adam

(eval (quote a)) => a

but

(quote (eval a) => (eval a)

so it's not inverse in the mathematical sense (half inverse maybe).

Jules
From: Zach Beane
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <m3ods0gxfm.fsf@unnamed.xach.com>
······@ieee.org writes:

> Hi:
> 
> Is it fair to say that eval is the opposite of quote?

Not really.

QUOTE is a special form that only has meaning during evaluation. In
situations where forms are not evaluated (macro arguments, for
example), QUOTE doesn't come into play, and the arguments are not said
to be "quoted". 

You could say that function arguments are evaluated and macro
arguments are not necessarily evaluated, but that doesn't imply that
the natural state of the world is to be evaluated and it is a
temporary exception to be unevaluated. It all depends on the
context. Specific macros may in fact evaluate some of their arguments
while leaving some arguments unevaluated.

For example, IN-PACKAGE is a macro that does not evaluate its
argument. That's why you can write (IN-PACKAGE FOO), and it won't
complain that the symbol FOO is not bound, no matter what. Here's the
source code for that IN-PACKAGE from SBCL:

   (defmacro-mundanely in-package (string-designator)
     (let ((string (string string-designator)))
       `(eval-when (:compile-toplevel :load-toplevel :execute)
          (setq *package* (find-undeleted-package-or-lose ,string)))))

Within the macro, the argument STRING-DESIGNATOR is bound to the
symbol FOO. It doesn't make sense to say that the symbol FOO is
"quoted" in this context; it's just a piece of data. It is then
converted to a string for use in the code IN-PACKAGE expands to, but
at no time is it treated as the name of a variable and evaluated to
produce some value.

On the other hand, DOLIST is a macro that *does* evaluate some of its
arguments:

   (dolist (i list)
     (print i))

In this case, I is not evaluated, LIST is evaluated, and (PRINT I) is
repeatedly evaluated with I bound to successive elements of LIST.

You can't, just by looking at a macro call, tell the rules by which it
will evaluate every argument. The documentation should explain which
arguments are evaluated and which are not.

Getting back to EVAL, it is not a general-purpose way to get some
value out of the un-evaluated source code passed to a macro. It can't
see the values of lexical variables. For a recent homework problem
designed to illustrate this, see:

   http://groups.google.com/groups?q=nth-expr

In other words, most of the time, a macro should be designed to work
with the structure and literal values of the code it receives, not
with the (eventual) values the variables in the code will have at
runtime.

(Your initial example, using a special variable, confuses the issue
somewhat. Maybe it would be clearer if you posted a more complete
portion of your source code.)

Everything everyone else has been saying is completely right,
too. Read it carefully, and maybe something will click.

Zach
From: Pascal Bourguignon
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <87u01spjwv.fsf@thalassa.informatimago.com>
······@ieee.org writes:

> Hi:
>
> Is it fair to say that eval is the opposite of quote?
>
> (setf a 3)
> --> 3
> (eval (quote a))
> --> 3

In (eval (quote a)), there is no quote.

EVAL is a function so it gets its argument _evaluated_ before it is called.
Evaluation of (QUOTE A) returns A.

So what (EVAL (QUOTE A)) does, 

is to call the function EVAL with the argument A : no quote!

Perhaps you want to try:

   (eval (quote (quote a)))

Evaluates (QUOTE A). It returns A, not 3.


> Is there another function considered the opposite of quote?

QUOTE is not a function, it's a special operator.
The purpose of QUOTE is to return its argument un-evaluated, as is.
That is, QUOTE does nothing.  When you read the source of EVAL, you see:

  (cond
      ...
      ((eq 'quote (first form)) (second form)) ; does nothing but return the
                                               ; argument unchanged.
      ...)

So what would be the opposite of doing nothing?  
Doing more nothing?

EVAL is fire, QUOTE is water.

Quote prevents the evaluation of its argument.

---------

Thery are not opposite in that (COMPOSE EVAL QUOTE) is not 
IDENTITY, no more than (COMPOSE QUOTE EVAL) is (COMPOSE EVAL QUOTE).

(eval (quote a)) --> 3
(quote (eval a)) --> (eval a)


(compose eval quote) is not the identity because you can quote things
that are not valid forms, or forms that aren't quines:

(eval (quote (3 4 5))) --> error, since (3 4 5) is not a form.
(eval (quote (/ 1 2))) --> 1/2 ; instead of (/ 1 2) which is not a quine.


---------

There's a functional simil of QUOTE : IDENTITY

(quote 3)       --> 3            (identity '3)       --> 3
(quote a)       --> a            (identity 'a)       --> a
(quote (+ 1 2)) --> (+ 1 2)      (identity '(+ 1 2)) --> (+ 1 2)

Of course, the opposite of identity is identity:

(let ((x '3))       (eq (identity (identity x)) x)) --> T
(let ((x 'a))       (eq (identity (identity x)) x)) --> T
(let ((x '(+ 1 2))) (eq (identity (identity x)) x)) --> T


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Cats meow out of angst
"Thumbs! If only we had thumbs!
We could break so much!"
From: Alan Crowe
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <86ac3k49iu.fsf@cawtech.freeserve.co.uk>
······@ieee.org writes:
> Is it fair to say that eval is the opposite of quote?
> 
> (setf a 3)
> --> 3
> (eval (quote a))
> --> 3
> 
> Is there another function considered the opposite of
> quote?

I tried writing a tutorial, a first Common Lisp lesson.
It turned out a bit peculiar with too much emphasis on eval,
so I abandoned it, but since you ask specifically about eval
you may find it to your taste:

Common Lisp as a first computer language

Type 2<RET>

The lisp interpreter looks at 2 and evaluates it. It
recognises it as a number, and applies the rule that numbers
evaluatie to themselves, so the answer is 2.

Type: (+ 2 2)<RET>

The computer sees the opening parenthesis and realises it is
being given a list. When it reaches the closing parenthesis
it is able to work out that it has seen a list of three
itmes.The first is the + sign, so it knows to add together
the values of the remaining items on the list. So it
evaluates them, the number 2 having the value 2 as
before. Answer: 4

Things to do wrong: 
0)Forget to type return. You always have to type return at
the end to tell the computer that you have finished your
turn and its is its go. So I won't be putting <RET> at the
end of any more lines.

1)Leave out the first space: (+2 2)
  Result: Illegal function call

2)Think its a typo and try: +(2 2)
  Error: Illegal function call.

3)Think its a typo and do the infix thing (2+2)
  Warning: This function is undefined:
  |2+2|

4)Put in spaces (2 + 2)
  Error: Illegal function call

First clarification exercise
type:(+ 1 2 3)
result 6

type:(+ 1 1 1 1 1 1 1)
7

Second clarification exercise
type:(+ 1 20 300 4000 50000)
result 54321

Instead of writing 1+20+300+4000+50000
one writes the plus sign as the first item of
a list that can be as long as you wish.

The list appears to be laid out like one might lay out a
shopping list: (potatoes carrots onions bread milk) with no
concession to the idea that + is part of arithmetic and a
bit special. Be careful though. The first location on the
list is special, and + has to come first.

Well that is addition. What about multiplication? There is
no times sign on the usual computer keyboard. There are lots
of possibilities, x or mult or times. In fact Lisp does the
same as FORTRAN and uses *

(* 5 7)
35

Can you use a list as long as you want? Yes.

(* 2 2 2)
8

(* 5 7 11)
385

(* 1 1 5 1 1 1 7 1 1 1 11)
385

(* 1 1 5 1 1 1 7 0 1 1 11)
0

Indeed, you can use lists as short as you want.

(+ 23)
23

(* 137)137

(+)
0

(*)
1

Why? There a depths here that must be defered until later.



Subtraction is as clunky in Lisp as in any other language.
(-) gives an error
(- 96) gives -96
(- 96 23) gives 73
(- 96 20 1 1 1)73
indeed (- a b c d e) is the same as (- a (+ b c d e))

Division contains a surprise.

Lisp does fractions

(+ 1/2 1/2)
1

(+ 1/2 1/3)
5/6

(+ 1/10 1/15)
1/6

(- 1/2 1/3)

(* 1/10 1/15)
1/150

(/ 1/10 1/15)
3/2(

This is potentially confusing

If you try (/ 2 3) you get 2/3 which is probably an
unpleasant surprise, if you were expecting 0.6666667. If you
had tried (/ 8 12) you would also have got 2/3, which might
have been a pleasant surprise. If you don't want a fraction,
you can always say

(float 8/12)
0.6666667

or

(float (/ 8 12))
0.6666667

Division works the same way as subtraction with 
(/ a b c d e) calculation (/ a (* b c d e))

This works OK in practise. A calculation such as
6x5x4/3x2x1 gets translated to the following piece of Lisp
(/ (* 6 5 4) 3 2 1)

Variables

In Lisp, variables have some extra features, and are called
symbols. A variable is a box containing a value. A symbol is
a somewhat larger box, with its name written on the side. A
symbol has two values, a general purpose value, and a
function value used instead in particular circumstances. You
can use the symbol as a thing in itself, without regard to
its value.

We start by setting a symbol's general purpose value.
There are several commands for setting the values of
symbols, set, setq, setf, psetq, psetf. One can get a long
way with just setf so we start with that one

(setf my-first-symbol 57)
57

This sets the general purpose value of the symbol
MY-FIRST-SYMBOL to 57, and returns 57. Now we can type

my-first-symbol
57

and 

(+ my-first-symbol 3)
60


(setf second-symbol (+ 20 3))
23

Well, plainly this has performed the calculation, and
returned the answer, but what did the general purpose value
of our second-symbol get set to? Have we used it to record
the calculation we requested, (+ 20 3), or the answer that
the computer calculated?

second-symbol
23

If we want to record the calculation for future reference we
must "quote" it. Think of the computer as a horse and the
quote as a bridle, reining it in, stopping it from rushing
on to evaluate things before you want it to.

(setf third (quote (+ 20 3)))
(+ 20 3)

Now

third
(+ 20 3)

the general purpose value of our third symbol contains a
calculation, which the computer 
is champing at the bit to execute.

If quote pulls on the reins, how do we get started again?
eval.

(eval third)
23

It is controversial to use eval in the first lesson because
it is seldom typed in, but I had to introduce quote, because
quote is typed in so often that it has a special
abreviation. One types

(setf third '(+ 20 3))
(+ 20 3)

Notice that this is a very special abreviation. Not only are
the five letters of quote shortened to the single charater
', but the brackets are also ommitted.

May I draw your attention to a nicety of language. I wrote
that eval is seldom typed in and quote is often typed in. I
refrained from writing that eval is seldom used, and quote
is often used. The point is that when you type (+ 20 3) the
interpreter does not echo (+ 20 3), impudently saying, "yeah
like so what, what do you expect me to do with that". The
interpreter evaluates it immediately, with out being
asked. In a sense, one is using eval all the time. One types
in quote quite often because one does not in fact want to be
using eval all the time, far from it. One seldom types in
eval because one seldom wants it done twice, one is much
more interested in pulling back on the reins, with quote, so
that eval is not done at all. Typing eval is like spurring
your computer to gallop.

We have set three symbols so far and are perhaps in danger
of forgetting what they contain. The function list builds a
list, eg

(list 1 2 3)
(1 2 3)

so lets build a list of the values of our three symbols

(list my-first-symbol second-symbol third)
(57 23 (+ 20 3))

There are two potential confusions here. Which value is
which. Perhaps we should turn off evaluation using quote

(list 'my-first-symbol my-first-symbol 'second-symbol
second-symbol 'third third)
(MY-FIRST-SYMBOL 57 SECOND-SYMBOL 23 THIRD (+ 20 3))


see what I mean about the name of the symbol being written
on the its box. 

The second and more serious confusion arise from comparing

(list 1 2 3)
(1 2 3)

and

(list my-first-symbol second-symbol third)
(57 23 (+ 20 3))

It looks as though list is somehow deciding whether or not
to evaluate its arguments, refraining in the first instance
and rushing ahead in the second.

With quote and eval we can investigate this. Let us evaluate
1 zero times, once, twice, and three times

'1
1

1
1

(eval 1)
1

(eval (eval 1))
1

Compare this to evaluation third, zero, one, two, and three
times.

'third
THIRD

third
(+ 20 3)

(eval third)
23

(eval (eval third))
23

Numbers are not symbols. There is no box containing two
values. They just are, and they evaluate to themselves.
Since numbers are not symbols, 

(setf 1 '1)

does not work. You can get a feel for what is going on gy
typing

(setf my-symbol-1 'my-symbol-1)

Now my-symbol-1 evaluates to itself. It shrugs off
evaluation just as numbers do.

'my-symbol-1
MY-SYMBOL-1

my-symbol-1
MY-SYMBOL-1

(eval my-symbol-1)
MY-SYMBOL-1

(eval (eval my-symbol-1))
MY-SYMBOL-1

I'm going to labour this point. I have a reason. The
metaphor of a variable as a box that contains things is a
good one. Much of the time the metaphor works well. You keep
something in the box for a while. Then you throw the
contents away and use the box to keep something else
instead. Unfortunately the metaphor is fundamentally
flawed. Both the box and its contents are
immaterial. Consider

(setf 4th third)

Has the list describing a simple calculation been put in the
box 4th?

4th
(+ 20 3)

Yes.

Has it been taken out of third?

third
(+ 20 3)

No.

Has it been copied? No. You make a copy like this:

(setf 5th (copy-list 4th))

Has it been moved? No. To the extent that the metaphor of
motion works at all, you move things like this

(setf 6th 5th 5th nil)

There is a command (shiftf 6th 5th nil) which writes nil to
5th after moving the contents to 6th, but it returns the old
contents of 6th, so it cannot be used on shiny new variables
with no contents.

If it has not been copied and it has not been moved, what
has happened? Something special to the immaterial world of
tangled boxes, which we will not explore today.

For a striking example do 

(setf red 'green)
(setf green 'blue)
(setf blue 'red)

now

'red
RED

red
GREEN

(eval red)
BLUE

(eval (eval red))
RED

Now for the key test. What happens with (+ 1 (* 2 3)) and
(+ 1 '(* 2 3)).

The second of these is easy to understand. We have used
quote to prevent evaluation, so (* 2 3) is a list of three
items, giving instructions for an arithmetical calculation
to be carried out at some later time. It is not the result
of the calculation it describes and is not a number. Sure
enough:

(+ 1 '(* 2 3))
Argument Y is not a NUMBER: (* 2 3).

By contrast 

(+ 1 (* 2 3))
7

appears cleverer than it really is. It looks like the
interpreter looks at its arguments and decides which ones to
evaluate. For example it looks as though

(+ 1 (* 2 3) (* 10 10) 30)
137

is realising that it needs to evaluate argument 2 and 3
while leaving 1 and 4 alone.

In fact

(* 2 3)
6

only looks like it is doing what you think. It is evaluating
2 and 3, getting 2 and 3 as the results of the two
evaluations, then multiplying the two results to get six.

When the interpreter evaluates (+ 1 (* 2 3)) it evaluates 1
and (* 2 3). 1 evaluates to 1, (* 2 3) evaluates to 6. Then
it adds them to get 7.

There is something worth pondering here. Get a cheap, old,
pocket calculator out of a draw and try 1 + 2 x 3
Typically, when you press x, the calculator, lacking an
extra register to hold pending results, carries out the
addition of the one and the two. One ends up calculating 3 x
3 and getting 9, rather than 7. Modern calculators follow
the standard rules of precendence, and defer performing the
addition until after they have multiplied 2 by 3, eventually
arriving at 7, as desired. 

Computer languages have many more operations than addition
and multiplication and often have elaborate systems of
precedence controlling with operation are carried out
first. Lisp has not such subtlety. One either renders

(1+2)x3 as (* (+ 1 2) 3)

or one renders

1+(2x3) as (+ 1 (* 2 3))

there is no way to preserve the ambiguity of

1+2x3

This turns out to be for the best in practice.


Obscure note
You could try (+ 1 * 2 3).
At the top level, * is used for recalling the result of the
previous command. If that was a number it will give the
wrong answer. If that was not a number the interpreter will
signal an error. Within a program, (+ 1 * 2 3) will generate
an error message saying that * has no value. More on this
later.

Let us return to third.
Remember that we set our third symbol to a list of three
items. We can see the whole list by typing third

third
(+ 20 3)

Lisp has functions for extracting items from a list. First
gets the first item

(first third)
+

The function second gets the second item on the list.

(second third)
20

If we would prefer multiplication we can change the symbol
at the start of the list

(setf (first third) '*)
* 

third
(* 20 3)

(eval third)
60

We can change the second item

(setf (second third) 7)
7

third
(* 7 3)

(eval third)
21

and, mysteriously, we can do the same with the third item

(third third)
3

(setf (third third) 4)

third
(* 7 4)

(eval third)
28

How does this work? Remember what I said earlier "A
symbol has two values, a general purpose value, and a
function value used instead in particular circumstances."

The particular circumstances are when eval is evaluating the
first symbol in a list. The function that eval applies, to
the result of evaluating the other items in the list, is the
function value of the symbol, not the general purpose value
of the symbol.

To make this clear, use symbol-function and symbol-value

(symbol-function 'third)
#<Function THIRD {103C7F19}>

(symbol-value 'third)
(* 7 4)

(symbol-function 'my-first-symbol)
Error in KERNEL:%COERCE-TO-FUNCTION:  the function MY-FIRST-SYMBOL is undefined.

(symbol-value 'my-first-symbol)
57

(symbol-function '+)
#<Function + {10295819}>


(symbol-value '+)
(SYMBOL-FUNCTION '+)

this is very confusing. The interpreter stores the last
command executed as the general purpose value of the symbol
+, so (symbol-value '+) depends on what you did last. In
side a program (symbol-val '+) will do something like

(symbol-value '=)
Error in KERNEL::UNBOUND-SYMBOL-ERROR-HANDLER:  the variable = is unbound.

just the same as

(symbol-value 'my-misspelled-simbol)
Error in KERNEL::UNBOUND-SYMBOL-ERROR-HANDLER:  the variable MY-MISSPELLED-SIMBOL is unbound.

All these error messages are quite annoying. Is there anyway
to avoid them? Yes. Boundp checks whether there is a general
purpose value, and fboundp checks whether there is a
function value,
 
(fboundp '+)
T

T is used for true

(fboundp 'my-first-symbol)
NIL

NIL is used for false, rather than F

(boundp 'my-first-symbol)
T 

(boundp 'my-misspelled-simbol)
NIL

Introductions to Lisp usually keep quiet about
symbol-function. I understand why. Now that I have told you
about it, you are equipped to wreck havock, and undertake
all sorts of devious mischief.

For example

(symbol-function '*)
#<Function * {1005F739}>

and

(symbol-function  '+)
#<Function + {10295819}>

give access to the functions that add and multiply.

Lets save them for later

(setf mult (symbol-function '*) add (symbol-function  '+))

Notice that you can set as many symbols as you want with a
single setf.

Notice also that I have put these functions in the general
purpose values of the symbols

(fboundp 'mult)
NIL

(boundp 'mult)
T

(symbol-value 'mult)
#<Function * {1005F739}>

Notice that 

mult
#<Function * {1005F739}>

works as well. I'm using symbol-value to make the parallel
to symbol-function more apparent.

You can store functions in general purpose value of a
symbol. It really is the general purpose the value of the
symbol, not the data value of the symbol.

(mult 4 5)
Warning: This function is undefined:
  MULT
Error in KERNEL:%COERCE-TO-FUNCTION:  the function MULT is undefined.

doesn't work. When eval tries to evaluate a list that starts
with a symbol, it looks for the function value of the
symbol, and signals an error if it cannot find one.

(funcall mult 4 5)
20

works. So does 

(apply mult '(4 5))
20

and

(apply mult (list 4 5)).
20

and

(apply add '(1 2 3 4))
10

It is worth remembering that apply subsumes funcall, ie all
of these work

(apply add '(1 2 3 4))
(apply add 1 '(2 3 4))
(apply add 1 2 '(3 4))
(apply add 1 2 3 '(4))
(apply add 1 2 3 4 '())

Back to mischief

(setf (symbol-function '+) mult)
(setf (symbol-function '*) add)

He he, you've got it, I'm swapping round + and *
(+ 5 7)
35

(* 25 75)
100

I'd better put them back

(setf (symbol-function '+) add (symbol-function '*) mult)

(+ 5 7)
12

(* 25 75)
1875

phew, thats better.

symbol-function is heavily used. So not only is there an
easier way of writing it, (function +) instead of
(symbol-function (quote +)), but the simplified way even has
its own abbreviation #'+. Err, thats not quite right, but it
will have to do for now.

Hope that helps

Alan Crowe
Edinburgh
Scotland
From: Timofei Shatrov
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <453f9fb7.48014170@news.readfreenews.net>
On 25 Oct 2006 17:58:49 +0100, Alan Crowe <····@cawtech.freeserve.co.uk> tried
to confuse everyone with this message:

>······@ieee.org writes:
>> Is it fair to say that eval is the opposite of quote?
>> 
>> (setf a 3)
>> --> 3
>> (eval (quote a))
>> --> 3
>> 
>> Is there another function considered the opposite of
>> quote?
>
>I tried writing a tutorial, a first Common Lisp lesson.
>It turned out a bit peculiar with too much emphasis on eval,
>so I abandoned it, but since you ask specifically about eval
>you may find it to your taste:
>
>Common Lisp as a first computer language
>

It actually looks quite useful. Can I add it to Wikibooks?

(http://en.wikibooks.org/wiki/Programming:Common_Lisp) 


-- 
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|
From: Rob Warnock
Subject: Re: newbie Q: opposite of quote
Date: 
Message-ID: <9eudnWJ3xtJgqt3YnZ2dnUVZ_tWdnZ2d@speakeasy.net>
Alan Crowe  <····@cawtech.freeserve.co.uk> wrote:
+---------------
| ······@ieee.org writes:
| > Is it fair to say that eval is the opposite of quote?
...
| > Is there another function considered the opposite of quote?
...
| Type 2<RET>
| 
| The lisp interpreter looks at 2 and evaluates it. It
| recognises it as a number, and applies the rule that numbers
| evaluatie to themselves, so the answer is 2.
| 
| Type: (+ 2 2)<RET>
| 
| The computer sees the opening parenthesis and realises it is
| being given a list. When it reaches the closing parenthesis
| it is able to work out that it has seen a list of three items.
| The first is the + sign, so it knows to add together the values
| of the remaining items on the list. So it evaluates them, the
| number 2 having the value 2 as before. Answer: 4
+---------------

CLHS "3.1.2.1 Form Evaluation" is sometimes useful at this point:

    http://www.alu.org/HyperSpec/Body/sec_3-1-2-1.html
    Forms fall into three categories: symbols, conses, and
    self-evaluating objects. The following sections explain these
    categories.

    3.1.2.1.1 Symbols as Forms

    3.1.2.1.2 Conses as Forms

    3.1.2.1.3 Self-Evaluating Objects


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607