Page 1 of 1

Problem in executing the code

Posted: Tue Nov 25, 2014 4:29 am
by vignezds
Hello Friends, Now I got some idea about Functions but I am not able to understand about macro's. For example here is a code:

Code: Select all

(defmacro reverse-list ( list )
    `(if (atom ,@list)
        ,@list 
      (reverse (mapcar #'reverse-list ,@list))))


`(reverse-list (list(1 2 3 4 5)))
I am not getting answer for this, what goes wrong here??, some one help me to sort out..

Re: Problem in executing the code

Posted: Tue Nov 25, 2014 2:15 pm
by logxor
Pro tip—you have no hope of understanding your own macros without MACROEXPAND-1. Try expanding the call form:

Code: Select all

? (macroexpand-1 `(reverse-list (list(1 2 3 4 5))))
(IF (ATOM LIST (1 2 3 4 5))
    LIST
    (1 2 3 4 5)
    (REVERSE (MAPCAR #'REVERSE-LIST LIST (1 2 3 4 5))))
Comma-at-sign is the splicing operator, so the list being spliced loses its identity and gets absorbed into the outer form.

Re: Problem in executing the code

Posted: Tue Nov 25, 2014 2:33 pm
by vignezds
Yes, as you said, I am very much new to this language, so kindly help me with some tutorials where I can find function example and corresponding macro

Re: Problem in executing the code

Posted: Tue Nov 25, 2014 4:19 pm
by porky11
i think, you should read books. there are some good ones online for free (like the ones Paul Graham wrote). (the only lang i learnt this way)
But if you understand the basics it should often be enough to read the entrys for the operations in common lisp hyperspec, there also are many examples.

Re: Problem in executing the code

Posted: Tue Nov 25, 2014 8:03 pm
by logxor

Re: Problem in executing the code

Posted: Wed Nov 26, 2014 10:20 am
by vignezds
Thank You for your suggestion. 'll go through it.