From: Frank Buss
Subject: Re: loop ... until ... and then return this
Date: 
Message-ID: <hbu68ba9u85z.1vh24o5nqtr4y.dlg@40tude.net>
Stefan Ram wrote:

> ( defun noun-phrase-start()
>   ( loop until( setq result( append-a-random-noun-to( a-random-determiner ))))
>   result )
> 
>   The intention is to return the first result of
>   "append-a-random-noun-to" that is not nil. Just for curiosity,
>   is there a more idiomatic way to write this?

My first idea:

(loop do (let ((result (append-a-random-noun-to (a-random-determiner))))
           (when result (return result))))

Note: if your variable "result" was not defined before with defparameter or
"let", it is not legal to use "setq" on it, because no binding exists for
it. Don't think that you can learn Lisp by just reading this newsgroup :-)

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de

From: Frank Buss
Subject: Re: loop ... until ... and then return this
Date: 
Message-ID: <1cnejdn30t8cz$.iuuvvwewvhg1.dlg@40tude.net>
Another good idea is to use anaphoric macros, as described in "On Lisp" by
Paul Graham:

(defmacro awhen (test-form &body body)
  `(let ((it ,test-form))
     (when it (progn ,@body))))

The trick of this macro is the variable capturing. Normally you don't want
this and you use gensym to avoid it, but for anaphoric macros it is very
useful:

(loop do
  (awhen (append-a-random-noun-to (a-random-determiner)) (return it)))

As Bulent wrote, loop provides something similar with "thereis", but the
"awhen" macro can be used for other problems, too.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Carl Shapiro
Subject: Re: loop ... until ... and then return this
Date: 
Message-ID: <ouyek2i36ux.fsf@panix3.panix.com>
Frank Buss <··@frank-buss.de> writes:

> (loop do
>   (awhen (append-a-random-noun-to (a-random-determiner)) (return it)))
>

The extended loop macro does this sort of thing out of the box.

  (loop for string in '("foo" "123" "bar" "456")
        when (parse-integer string :junk-allowed t)
          collect it)
  => (123 456)
From: Coby Beck
Subject: Re: loop ... until ... and then return this
Date: 
Message-ID: <1geFf.168191$AP5.41922@edtnps84>
"Carl Shapiro" <········@panix.com> wrote in message 
····················@panix3.panix.com...
> Frank Buss <··@frank-buss.de> writes:
>
>> (loop do
>>   (awhen (append-a-random-noun-to (a-random-determiner)) (return it)))
>>
>
> The extended loop macro does this sort of thing out of the box.
>
>  (loop for string in '("foo" "123" "bar" "456")
>        when (parse-integer string :junk-allowed t)
>          collect it)
>  => (123 456)

That's the only time I have seen that kind of use of "it" that I liked. 
Maybe because it is inside the LOOP form where things are already in another 
world and it fits...

-- 
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")