Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!decvax!decwrl!ucbvax!CSNET-RELAY.ARPA!Cerys%TILDE%ti-csl.CSNET From: Cerys%TILDE%ti-csl.CSNET@CSNET-RELAY.ARPA.UUCP Newsgroups: mod.ai Subject: Re: Common LISP style standards Message-ID: Date: Sun, 1-Jun-86 17:52:27 EDT Article-I.D.: Newton.Cerys.2727028344 Posted: Sun Jun 1 17:52:27 1986 Date-Received: Tue, 3-Jun-86 21:34:51 EDT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The ARPA Internet Lines: 34 Approved: ailist@sri-ai.arpa Date: 15 May 86 17:42:18 GMT From: tektronix!uw-beaver!ssc-vax!bcsaic!michaelm@ucbvax.berkeley.edu (michael maxwell) A common situation we find ourselves in is the following. We have a long list, and we wish to apply some test to each member of the list. However, at some point in the list, if the test returns a certain value, there is no need to look further: we can jump out of processing the list right there, and thus save time. Now you can jump out of a do loop with "(return )", but you can't jump out of a mapc (mapcar etc.) with "return." So we wind up using "do" a lot of places where it would otherwise be natural to use "mapcar". I suppose I could use "catch" and "throw", but that looks so much like "goto" that I feel sinful if I use that solution... Any style suggestions? It sounds like the function you want is MEMBER-IF. This takes two required arguments, a predicate and a list. As soon as the predicate succeeds on one of the elements of the list, the tail of the list is returned, else NIL is returned. There is nothing wrong with using DO or any of the mapping functions, as long as you are using the "best" function for the task. In the case you've described, MEMBER-IF is perfect because it immediately conveys to the reader (which may be yourself months after you've written it) what is being tested for. DOs and RETURNs can hide this meaning. Another useful variant of DO is DOLIST, which is similar (and prefered by many) to MAPC. Within our group, we prefer to use the mapping functions only where they appear to be "natural" to the task (eg, list transformations). But granted, what is "best" and "natural" depends a lot on your background and approach to Lisp.