Bernie Simpsons
Bernie Simpsons

Reputation: 31

lisp function that doesn't evaluate his arguments

For my homework, I have a small List interpreter written in C with few functions already coded : set, cons, car, cdr and quote.

And I have to add 6 another functions of my choice. 3 with argument evaluation, and 3 without argument evaluation. With evaluation, I choose : eq, + and -1, but I don't have any idea for the 3 next, what kind of Lisp function is useful without argument evaluation ?

Upvotes: 1

Views: 767

Answers (1)

sds
sds

Reputation: 60014

The need for deferred evaluation

All arguments to lisp functions are always evaluated.

This is not the case for macros and special operators, one of which, quote, you have already implemented.

The standard example when deferred evaluation is useful is:

(if (under-attack-p)
    (launch-missiles)
    (print "Peace!"))

I the arguments of if were evaluated, then we would ...

  1. check whether we are under attack
  2. print Peace!
  3. launch the missiles
  4. if we were under attack, return Peace! (the value returned by print), otherwise return the value of launch-missiles

However, the special operator if evaluates its first argument and decides which of the other two argument to evaluate based on that value.

See also How does `if` not evaluate all its arguments?

My recommendation for implementation is:

Upvotes: 4

Related Questions