From: ···········@gmail.com
Subject: How to count forms?
Date: 
Message-ID: <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?

From: Anon
Subject: Re: How to count forms?
Date: 
Message-ID: <xfOdndVn94cbWzjZnZ2dnUVZ_rKdnZ2d@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))))
From: Anon
Subject: Re: How to count forms?
Date: 
Message-ID: <D76dnUHzMcYtVzjZnZ2dnUVZ_oGdnZ2d@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.
From: Anon
Subject: Re: How to count forms?
Date: 
Message-ID: <-N6dnQFOwb_FQzjZnZ2dnUVZ_sydnZ2d@comcast.com>
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)))
From: Stefan Mandl
Subject: Re: How to count forms?
Date: 
Message-ID: <4glvk5F1mtmimU1@news.dfncis.de>
> (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