Trying to learn lisp using GNU common lisp, and I have a question:
Is there a way to count the number of forms passed to a macro? For
example, I have this code:
(defmacro my-test (&body body)
`(progn
(format t "Unknown number of forms in body~%")
,@body))
(my-test (format t "A~%")(format t "B~%")(format t "C~%"))
Is there a way to have that print out a line that says "3 forms in
body" based on the number of forms passed to the macro?
···········@gmail.com wrote in news:1151713951.284994.143220
@d30g2000cwa.googlegroups.com:
> Trying to learn lisp using GNU common lisp, and I have a question:
>
> Is there a way to count the number of forms passed to a macro? For
> example, I have this code:
>
> (defmacro my-test (&body body)
> `(progn
> (format t "Unknown number of forms in body~%")
> ,@body))
>
> (my-test (format t "A~%")(format t "B~%")(format t "C~%"))
>
> Is there a way to have that print out a line that says "3 forms in
> body" based on the number of forms passed to the macro?
>
>
(defmacro my-test (&body body)
`(format t "~D form~:P in body" (length (quote ,body))))
Anon <·······@pvofotpmaq.net> wrote in
·····································@comcast.com:
> ···········@gmail.com wrote in news:1151713951.284994.143220
> @d30g2000cwa.googlegroups.com:
>
>> Trying to learn lisp using GNU common lisp, and I have a question:
>>
>> Is there a way to count the number of forms passed to a macro? For
>> example, I have this code:
>>
>> (defmacro my-test (&body body)
>> `(progn
>> (format t "Unknown number of forms in body~%")
>> ,@body))
>>
>> (my-test (format t "A~%")(format t "B~%")(format t "C~%"))
>>
>> Is there a way to have that print out a line that says "3 forms in
>> body" based on the number of forms passed to the macro?
>>
>>
>
>
> (defmacro my-test (&body body)
> `(format t "~D form~:P in body" (length (quote ,body))))
>
geez, let the forms evaluate too :)
(defmacro my-test (&body body)
`(progn
(format t "~D form~:P in body~%" (length (quote ,body)))
,@body))
something like that maybe.
Anon <·······@pvofotpmaq.net> wrote in
·····································@comcast.com:
> Anon <·······@pvofotpmaq.net> wrote in
> ·····································@comcast.com:
>
>> ···········@gmail.com wrote in news:1151713951.284994.143220
>> @d30g2000cwa.googlegroups.com:
>>
>>> Trying to learn lisp using GNU common lisp, and I have a question:
>>>
>>> Is there a way to count the number of forms passed to a macro? For
>>> example, I have this code:
>>>
>>> (defmacro my-test (&body body)
>>> `(progn
>>> (format t "Unknown number of forms in body~%")
>>> ,@body))
>>>
>>> (my-test (format t "A~%")(format t "B~%")(format t "C~%"))
>>>
>>> Is there a way to have that print out a line that says "3 forms in
>>> body" based on the number of forms passed to the macro?
>>>
>>>
>>
>>
>> (defmacro my-test (&body body)
>> `(format t "~D form~:P in body" (length (quote ,body))))
>>
>
> geez, let the forms evaluate too :)
>
> (defmacro my-test (&body body)
> `(progn
> (format t "~D form~:P in body~%" (length (quote ,body)))
> ,@body))
>
> something like that maybe.
this version tries to count the forms inside other foms too.
(defmacro my-test (&body body)
(labels ((count-forms (ls)
(cond
((null ls) 0)
((listp ls)
(cond
((listp (car ls))
(+ 1
(count-forms (car ls))
(count-forms (cdr ls))))
(t (count-forms (cdr ls)))))
(t 0))))
`(progn
(format t "~D form~:P in body~%" ,(count-forms body))
,@body)))
> (my-test (format t "A~%")(format t "B~%")(format t "C~%"))
>
> Is there a way to have that print out a line that says "3 forms in
> body" based on the number of forms passed to the macro?
CL-USER> (defmacro my-test (&body body)
`(progn
(format t "~d forms in body~%" ,(length body))
,@body))
CL-USER> (my-test (format t "A~%")(format t "B~%")(format t "C~%"))
number of forms in body=3
A
B
C
NIL