Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Macro-writing challenge

Name: Anonymous 2016-01-16 13:19

Write a macro check-arguments-non-nil to be used in function definitions, that would expand to assertion about every argument being non-nil.
The intended usage of this macro:

(defun foo (a b c d)
(check-arguments-non-nil)
foo-body)


This should expand to:

(defun foo (a b c d)
(assert (and (not (null a)) (not (null b)) (not (null c)) (not (null d)))
(a b c d)
"Null argument in FOO")
foo-body)


The macro must work whatever the number and names of the function parameters.

Name: Anonymous 2016-01-21 0:08

>>44
C preprocessor macros are just string replacement. Lisp macros are full functions which receive an AST and return an AST, and have the full power of the language available at "compile time". The various quotes and commas are standard Lisp list processing shorthand, which apply to source code AST just as they would any other list-based "normal" data.

About the only time you'll be able to do something in a C macro that you can't 100% directly do in Lisp is when your macro leaves an open brace or something sub-syntactic like that, expecting to use multiple C macros in tandem at the beginning and end of a block. In that case, the Lisp macro would simply wrap the full body, doing whatever it wants to the beginning, end, or even nested contents of the code, lexically and semantically containing the scope of the code it's affecting.

(defmacro run-macro-2 (macro expr1 expr2 &rest args)
`(progn
,expr1
(setf ,expr2 (,macro ,@args))))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List