Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a macro definition and function definition

I'm trying to learn Lisp but I got stuck by this example (you can find it on "ANSI Common Lisp", by Paul Graham, page 170):

(defmacro in (obj &rest choices)
    (let ((insym (gensym)))
        `(let ((,insym ,obj))
             (or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
                           choices)))))

Graham then states:

The second macro [...] in returns true if its first argument is eql to any of the other arguments. The Expression that we can write as:

(in (car expr) '+ '- '*)

we would otherwise have to write as

(let ((op (car expr)))
    (or (eql op '+)
        (eql op '-)
        (eql op '*)))

Why I should write a macro when the following function I wrote seems to behave in the same way?

(defun in-func (obj &rest choices)
    (dolist (x choices)
        (if (eql obj x)
            (return t))))

I do not understand if I am missing something or, in this case, in-func is equivalent to in.

like image 278
vrde Avatar asked May 03 '11 23:05

vrde


People also ask

What is a function macro definition?

Function-like macro definition: An identifier followed by a parameter list in parentheses and the replacement tokens. The parameters are imbedded in the replacement code. White space cannot separate the identifier (which is the name of the macro) and the left parenthesis of the parameter list.

What is difference between using a macro and a in line function?

In the case of inline, the arguments are evaluated only once. Whereas in the case of macro, the arguments are evaluated every time whenever macro is used in the program.

What is the macro definition in computer?

2) In computers, a macro (for "large"; the opposite of "micro") is any programming or user interface that, when used, expands into something larger. The original use for "macro" or "macro definition" was in computer assembler language before higher-level, easier-to-code languages became more common.

Are macros just functions?

No; since macros work at the syntactical or lexical level; they can be used in contexts where functions cannot; and can accept arguments that functions cannot.


2 Answers

The difference in using the macro vs function is in whether all the choices always are evaluated.

The expanded in macro evaluates the choices sequentially. If it reaches a choice that is eql to the first argument, it returns a true value without evaluating any more forms.

In contrast, the in-func function will evaluate all the choices at the time the function is called.

like image 56
Terje Norderhaug Avatar answered Oct 03 '22 20:10

Terje Norderhaug


The two other answers are correct, but to make things more concrete, consider this interaction:

CL-USER(1): (defmacro in ...)
IN
CL-USER(2): (defun in-func ...)
IN-FUNC
CL-USER(3): (defvar *count* 0)
*COUNT*
CL-USER(4): (defun next () (incf *count*))
NEXT
CL-USER(5): (in 2 1 2 3 (next))
T
CL-USER(6): *count*
0
CL-USER(7): (in-func 2 1 2 3 (next))
T
CL-USER(8): *count*
1
like image 39
Eli Barzilay Avatar answered Oct 03 '22 22:10

Eli Barzilay