From: dstein64
Subject: Returning no value
Date: 
Message-ID: <d41ee186-5473-4028-9049-7652f419428f@w7g2000hsa.googlegroups.com>
How do I have a function return no value (not even nil). I am
collecting elements using a loop. In the body, there is an if
statement, that returns a specific value in some instances and returns
nil otherwise. I do not want the 'collect' to collect the nils. That
is, I would like to have the else-form of my conditional return no
value, not nil. Thanks.

From: Geoffrey Summerhayes
Subject: Re: Returning no value
Date: 
Message-ID: <2ef4f942-e03a-4e89-9e9a-242b9d19602b@26g2000hsk.googlegroups.com>
On Apr 30, 2:27 pm, dstein64 <········@gmail.com> wrote:
> How do I have a function return no value (not even nil). I am
> collecting elements using a loop. In the body, there is an if
> statement, that returns a specific value in some instances and returns
> nil otherwise. I do not want the 'collect' to collect the nils. That
> is, I would like to have the else-form of my conditional return no
> value, not nil. Thanks.

(defun foo()
  (if (< 5 (random 10))
      nil
    (values)))

(loop for x below 10 appending (multiple-value-list (foo)))

---
Geoff
From: globalrev
Subject: Re: Returning no value
Date: 
Message-ID: <b4d9ed7c-cfb5-44de-87d7-4f355864695d@a23g2000hsc.googlegroups.com>
On 30 Apr, 20:42, Geoffrey Summerhayes <·······@gmail.com> wrote:
> On Apr 30, 2:27 pm, dstein64 <········@gmail.com> wrote:
>
> > How do I have a function return no value (not even nil). I am
> > collecting elements using a loop. In the body, there is an if
> > statement, that returns a specific value in some instances and returns
> > nil otherwise. I do not want the 'collect' to collect the nils. That
> > is, I would like to have the else-form of my conditional return no
> > value, not nil. Thanks.
>
> (defun foo()
>   (if (< 5 (random 10))
>       nil
>     (values)))
>
> (loop for x below 10 appending (multiple-value-list (foo)))
>
> ---
> Geoff


(defun foo()
  (if (< 5 (random 10))
      nil
    (values)))

CL-USER> (loop for x below 10 appending (multiple-value-list (foo)))
(NIL NIL NIL NIL NIL)
From: Alex Mizrahi
Subject: Re: Returning no value
Date: 
Message-ID: <4819bb8f$0$90272$14726298@news.sunsite.dk>
 g> (defun foo()
 g>   (if (< 5 (random 10))
 g>       nil
 g>     (values)))

 g> CL-USER> (loop for x below 10 appending (multiple-value-list (foo)))
 g> (NIL NIL NIL NIL NIL)

do you think it's wrong in some way, or you just like this code so much that 
you want to show us run results? 
From: Thomas A. Russ
Subject: Re: Returning no value
Date: 
Message-ID: <ymizlrbt4o6.fsf@blackcat.isi.edu>
dstein64 <········@gmail.com> writes:

> How do I have a function return no value (not even nil). I am
> collecting elements using a loop. In the body, there is an if
> statement, that returns a specific value in some instances and returns
> nil otherwise. I do not want the 'collect' to collect the nils. That
> is, I would like to have the else-form of my conditional return no
> value, not nil. Thanks.

Well, the standard way to not return any values is to use the (values)
form.  But this will be intrepreted the same as NIL in any context in
which a value is needed.

So, for example:

  (list (values) 1 (values)) =>  (NIL 1 NIL)

That means that there really is nothing you can pass to collect that
will not be interpreted as a value of some type.  That is because
collect is not defined to accept an empty value.

Several solutions have already been posted.  One variant that I haven't
seen is the standard appending one, which is similar to the
multiple-values-list answer.  What you do is wrap an extra level of list
around your answers and then append or NCONC the results, which causes
NIL values to be skipped.

(defun foo (x)
  (cond ((not (numberp x)) nil)
        ((< x 0) (list (- x)))
        (t (list x))))

(loop for item in '(1 2 -3 foo bar -4 5)
      append (foo item))

  =>  (1 2 3 4 5)

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Steven M. Haflich
Subject: Re: Returning no value
Date: 
Message-ID: <481A714D.3080606@alum.mit.edu>
I think the OP may be looking for something like this:

cl-user(10): (defun foo()
	      (let ((x (random 10)))
		(and (< 5 x) x)))
foo
cl-user(11): (loop repeat 10
		as x = (foo)
		when x collect x)   ; <<<<<
(6 8 9)
From: Ken Tilton
Subject: Re: Returning no value
Date: 
Message-ID: <481a7ff0$0$25020$607ed4bc@cv.net>
Steven M. Haflich wrote:
> I think the OP may be looking for something like this:
> 
> cl-user(10): (defun foo()
>           (let ((x (random 10)))
>         (and (< 5 x) x)))
> foo
> cl-user(11): (loop repeat 10
>         as x = (foo)
>         when x collect x)   ; <<<<<
> (6 8 9)

Sweet. But not wnat someone already offered?:

   (loop repeat 10
         when (foo)
         collect it)

As for the use of as, C'ers flame over placing braces in an if statement 
-- has there ever been a Lisp flamewar over for vs. as?

If not, is it possible there is a limit to our collective Usenet idiocy? 
I feel we are on the verge of an important result.

Do you have a rationale for preferring as over for? I can guess at one 
but do not want to compromise the research.

Do you use for with in and as with = or always go with as? If so, then 
likewise with for and not as with on, across, over, below, and under?

kenny

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/

"I've never read the rulebook. My job is to catch the ball."
   -- Catcher Josh Bard after making a great catch on a foul ball
and then sliding into the dugout, which by the rules allowed the
runners to advance one base costing his pitcher a possible shutout
because there was a runner on third base.

"My sig is longer than most of my articles."
   -- Kenny Tilton
From: Zach Beane
Subject: Re: Returning no value
Date: 
Message-ID: <m38wyvgpde.fsf@unnamed.xach.com>
dstein64 <········@gmail.com> writes:

> How do I have a function return no value (not even nil). I am
> collecting elements using a loop. In the body, there is an if
> statement, that returns a specific value in some instances and returns
> nil otherwise. I do not want the 'collect' to collect the nils. That
> is, I would like to have the else-form of my conditional return no
> value, not nil. Thanks.

One option:

  (loop ...
        when (<your body with an if>)
        collect it)

Zach
From: Philippe Mechaï
Subject: Re: Returning no value
Date: 
Message-ID: <87ve1zfatb.fsf@free.fr>
Use a when clause in the loop.
Something like:
(loop for elt in lst
      when (not (null elt))
      collect elt)