DUjio
DUjio

Reputation: 33

Unexpected eval result in CLISP

I run following code of CLISP, but the result looks strange to me.

(setq a 'b)
(setq b 'c)
(setq c 'd)
(setq d 8)
(eval a)
(eval c)
(eval (eval a))

The output of last three line is:

C

8

D

How do I understand the output?

How could last two line have different output?

Please help explain this, thank you so much!

Upvotes: 3

Views: 68

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139241

Evaluate (eval c)

  1. get value of variable c -> symbol D
  2. call EVAL with symbol D -> number 8

Evaluate (eval (eval a))

  1. get value of variable a -> symbol B
  2. call EVAL with symbol B -> symbol C
  3. call EVAL with symbol C -> symbol D

Some basic evaluation rules for Lisp

  • a symbol evaluates to its value
  • a number evaluates to itself
  • a list (foo-function arg) evaluates first the argument and then calls the function foo-function with that evaluated argument
  • a list (quote something) returns something (whatever it is) as it is

Upvotes: 4

Related Questions