From: Tobias Maschler
Subject: problems with apply and calling macros
Date: 
Message-ID: <MASCHLER.95Sep26121924@pik.isr.uni-stuttgart.de>
Hello!

I want to check a list though, if all their elements are sublists:

'((a b) (g) (d e (gh))) --> t
'(a (b) (c d e f))      --> nil

So I wanted to use the following function combination:

	(apply #'and (mapcar #'listp '((a) (b) (v b s))))

(mapcar #'listp '((a) (b) (v b s))) returns (T T T) and 
(apply #'and ...) should make a conjunction (and T T T) of the
following arguments: (and T T T) --> T.
This doesn't work, because "and" is defined as a macro.

Do you know other solution ways to 
 * check if all elements of a list are sublists?
I already thought of programming the function recursively, but it
should work quickly, so if would prefer an iterative solution, because
the argument list will never contain more than 10 arguments.

Thank you very much,
			Yours,
				:) Tobias Maschler 

+--------------------------------------------------------------------+
| Tobias Maschler, Institut f"uer Systemdynamik und Regelungstechnik |
| Universitaet Stuttgart, Germany				     |
+--------------------------------------------------------------------+

From: Ralf Muschall
Subject: Re: problems with apply and calling macros
Date: 
Message-ID: <44chkv$jfb@fsuj01.rz.uni-jena.de>
In article <······················@pik.isr.uni-stuttgart.de> ········@isr.uni-stuttgart.de (Tobias Maschler) writes:

>'((a b) (g) (d e (gh))) --> t
>'(a (b) (c d e f))      --> nil

>So I wanted to use the following function combination:

>	(apply #'and (mapcar #'listp '((a) (b) (v b s))))

>This doesn't work, because "and" is defined as a macro.

Yes, that's what the FAQ says.

Try the general solution for this kind of trouble
(applying macros, that is, not checking for listp):

(reduce #'(lambda (x y) (and x y)) (mapcar #'listp '((a) (b) (c d e))))

this works, but is ugly. Of course, this removes the shortcut operation
of ``and'' (compare 
(and nil (/ 1 0))
and 
(funcall #'(lambda (x y) (and x y)) nil (/ 1 0))
).  A better solution (which avoids the ``and'' at all) has already
been posted by somebody else.

If you want to use apply instead of reduce (which is limited
to functions of 2 arguments), you will need something more
complicated than the lambda expression I put there.

Ralf

--
-- 
to get PGP keys: finger ···@cnve.rz.uni-jena.de (or from public key servers)
pgp public key fingerprints:
A3 93 A6 0B E2 8F 04 25  5B 3C 00 F6 55 E5 64 20  [1024 bit]
64 36 31 D8 7A 73 20 F7  CA 6A DA 02 AF 29 46 1C  [2047 bit]
From: Simon Brooke
Subject: Re: problems with apply and calling macros
Date: 
Message-ID: <44etk2$att@caleddon.dircon.co.uk>
"Stefan Monnier" <··············@epfl.ch> wrote:
>In article <······················@pik.isr.uni-stuttgart.de>,
>Tobias Maschler <········@isr.uni-stuttgart.de> wrote:
>] 	(apply #'and (mapcar #'listp '((a) (b) (v b s))))
>
>I guess you could just use
>
>	(every #'listp '((a) (b) (v b s)))
>
>or maybe you're not using CommonLisp ?

And if you aren't using CL, a very handy trick to have up your sleeve for 
such occasions (which in my experience are quite common) is 

(remove T (mapcar whatever whatever))

or of course, in the opposite case, (remove nil (mapcar ...

-- 
------- ·····@rheged.dircon.co.uk (Simon Brooke)

	How many pentium designers does it take to change a lightbulb?
		1.99904274017