From: Mirko
Subject: getting rid of nil
Date: 
Message-ID: <57dca1d1-4b66-42bf-b42b-1886e0127a61@h30g2000vbr.googlegroups.com>
Is there a way to rewrite

>(let (a)
   (if a `(5 6 ,a)
       `(5 6)))
(5 6)

into

>(let ((a nil))
   `(5 6 ,(when a a)))
(5 6 NIL)
?

I would like to get rid of the NIL.  I tried, obviously, if.  Anything
else, or any other approach?

Thank you

Mirko

From: Slobodan Blazeski
Subject: Re: getting rid of nil
Date: 
Message-ID: <9ff7b552-057d-4886-bcb1-ed8606bb2362@c36g2000yqn.googlegroups.com>
On Jun 30, 7:40 pm, Mirko <·············@gmail.com> wrote:
> Is there a way to rewrite
>
> >(let (a)
>
>    (if a `(5 6 ,a)
>        `(5 6)))
> (5 6)
>
> into
>
> >(let ((a nil))
>
>    `(5 6 ,(when a a)))
> (5 6 NIL)
> ?
>
> I would like to get rid of the NIL.  I tried, obviously, if.  Anything
> else, or any other approach?
>
> Thank you
>
> Mirko

What's the point of this, since a is always nil why don't you just use
(5 6) ?

Slobodan
From: Kenneth Tilton
Subject: Re: getting rid of nil
Date: 
Message-ID: <4a4a5a05$0$31273$607ed4bc@cv.net>
Slobodan Blazeski wrote:
> On Jun 30, 7:40 pm, Mirko <·············@gmail.com> wrote:
>> Is there a way to rewrite
>>
>>> (let (a)
>>    (if a `(5 6 ,a)
>>        `(5 6)))
>> (5 6)
>>
>> into
>>
>>> (let ((a nil))
>>    `(5 6 ,(when a a)))
>> (5 6 NIL)
>> ?
>>
>> I would like to get rid of the NIL.  I tried, obviously, if.  Anything
>> else, or any other approach?
>>
>> Thank you
>>
>> Mirko
> 
> What's the point of this, since a is always nil why don't you just use
> (5 6) ?

The example was distilled from a real-world case in which a might not be 
nil.

kt
From: Mirko
Subject: Re: getting rid of nil
Date: 
Message-ID: <1adb52fc-4952-49cc-9a16-1de2c277af8e@b9g2000yqm.googlegroups.com>
On Jun 30, 2:31 pm, Kenneth Tilton <·········@gmail.com> wrote:
> Slobodan Blazeski wrote:
> > On Jun 30, 7:40 pm, Mirko <·············@gmail.com> wrote:
> >> Is there a way to rewrite
>
> >>> (let (a)
> >>    (if a `(5 6 ,a)
> >>        `(5 6)))
> >> (5 6)
>
> >> into
>
> >>> (let ((a nil))
> >>    `(5 6 ,(when a a)))
> >> (5 6 NIL)
> >> ?
>
> >> I would like to get rid of the NIL.  I tried, obviously, if.  Anything
> >> else, or any other approach?
>
> >> Thank you
>
> >> Mirko
>
> > What's the point of this, since a is always nil why don't you just use
> > (5 6) ?
>
> The example was distilled from a real-world case in which a might not be
> nil.
>
> kt

Indeed, here is the real code (before the application of the above
recipe):

  (apply #'nlisp:plot `(,data ,time ,@(when legends `
(:legends ,legends))))))

Mirko
From: gugamilare
Subject: Re: getting rid of nil
Date: 
Message-ID: <80208da4-dd58-4dc6-8f54-0bb9611edcf3@k8g2000yqn.googlegroups.com>
On 30 jun, 22:03, Mirko <·············@gmail.com> wrote:
>
> Indeed, here is the real code (before the application of the above
> recipe):
>
>   (apply #'nlisp:plot `(,data ,time ,@(when legends `
> (:legends ,legends))))))
>
> Mirko

Why not

(apply #'nlisp:plot data time (when legengs `(:legends ,legends)))

?
From: Rob Warnock
Subject: Re: getting rid of nil
Date: 
Message-ID: <1sOdndJYEtJgKdfXnZ2dnUVZ_gSdnZ2d@speakeasy.net>
Kenneth Tilton  <·········@gmail.com> wrote:
+---------------
| Slobodan Blazeski wrote:
| > Mirko <·············@gmail.com> wrote:
| >> Is there a way to rewrite
| >>> (let ((a nil))
| >>    `(5 6 ,(when a a)))
| >> (5 6 NIL)
| >>
| >> I would like to get rid of the NIL.
...
| > What's the point of this, since a is always nil why don't you just use
| > (5 6) ?
| 
| The example was distilled from a real-world case in which a might not be 
| nil.
+---------------

I actually run into this quite frequently with macros that call
functions that use a SUPPLIED-P arg to distinguish explicit NIL
values from "missing" NIL values in &OPTIONAL and &KEY args.
The ,@(WHEN FOO-P (LIST FOO)) and ,@(WHEN FOO-P (LIST :FOO FOO))
idioms handle this nicly.


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Kaz Kylheku
Subject: Re: getting rid of nil
Date: 
Message-ID: <20090713184501.88@gmail.com>
On 2009-07-01, Rob Warnock <····@rpw3.org> wrote:
> Kenneth Tilton  <·········@gmail.com> wrote:
> +---------------
>| Slobodan Blazeski wrote:
>| > Mirko <·············@gmail.com> wrote:
>| >> Is there a way to rewrite
>| >>> (let ((a nil))
>| >>    `(5 6 ,(when a a)))
>| >> (5 6 NIL)
>| >>
>| >> I would like to get rid of the NIL.
> ...
>| > What's the point of this, since a is always nil why don't you just use
>| > (5 6) ?
>| 
>| The example was distilled from a real-world case in which a might not be 
>| nil.
> +---------------
>
> I actually run into this quite frequently with macros that call
> functions that use a SUPPLIED-P arg to distinguish explicit NIL
> values from "missing" NIL values in &OPTIONAL and &KEY args.
> The ,@(WHEN FOO-P (LIST FOO)) and ,@(WHEN FOO-P (LIST :FOO FOO))
> idioms handle this nicly.

There is an excellent reason why the third argument to IF is optional,
even though we have WHEN.

This is functional: we are not conditionally executing a body of
statements, but computing a result.

Also, why switch to explicit list construction in the middle of a backquote
job?

If there is an idiom to be found for this, that distinction ought to go

  ,@(if x `(,y))
From: Vassil Nikolov
Subject: extending the `comma' read macro (Ex: getting rid of nil)
Date: 
Message-ID: <snzy6r7fy1g.fsf_-_@luna.vassil.nikolov.name>
Let's scale up the example a tad and make it this: construct (FOO) if
FOO is unbound, otherwise (FOO BAR), where FOO => BAR.  With S the
symbol in question, there are at least the following variants:

  `(,s ,@(and (boundp s) `(,(symbol-value s))))
  `(,s ,@(if (boundp s) `(,(symbol-value s))))
  `(,s ,@(if (boundp s) `(,(symbol-value s)) '()))
  `(,s ,@(when (boundp s) `(,(symbol-value s))))
  (if (boundp s) `(,s ,(symbol-value s)) `(,s))

and I do not find any of these completely satisfactory from the point
of view of aesthetics and style.  It seems to me that a better (though
not out-of-the-box) way of doing this would be to extend the `comma'
read macro to have

  ,?<test-form> <value-form>

be equivalent to

  ,<value-form>

if <test-form> evaluates to true, and to

  ,@nil

otherwise [*].  Thus the above example could be expressed as

  `(,s ,?(boundp s) (symbol-value s))

(I am guessing that introducing such a new meaning for `?' in this
context will affect few existing programs, if that, and there would be
an easy workaround in escaping the question mark, if nothing else).

_________
[*] I have not experimented with this, so "seems" must be taken
    literally in this sentence.

Notes:

1. This syntax is "isomorphic" to the `#+' syntax.

2. I also considered

  ,[<test-form> <value-form>]

(where the mnemonic is the convention of putting optional elements in
square brackets), but this seems to require an infix marker as well,
like

  ,[<test-form> -> <value-form>]

which seems out of place (though this can be generalized to a number
of test-value pairs, like M-expr conditionals).

---Vassil.

P.S. If we managed to do what the original subject said, i.e. get rid
of nothing, the next logical step would be to get rid of something as
well, which would leave us with nothing again, making the circle
complete...


-- 
"Even when the muse is posting on Usenet, Alexander Sergeevich?"
From: Alessio Stalla
Subject: Re: extending the `comma' read macro (Ex: getting rid of nil)
Date: 
Message-ID: <79ede368-ff41-4316-8046-375e82b3bb3c@z14g2000yqa.googlegroups.com>
On Jul 2, 7:34 am, Vassil Nikolov <········@pobox.com> wrote:
> Let's scale up the example a tad and make it this: construct (FOO) if
> FOO is unbound, otherwise (FOO BAR), where FOO => BAR.  With S the
> symbol in question, there are at least the following variants:
>
>   `(,s ,@(and (boundp s) `(,(symbol-value s))))
>   `(,s ,@(if (boundp s) `(,(symbol-value s))))
>   `(,s ,@(if (boundp s) `(,(symbol-value s)) '()))
>   `(,s ,@(when (boundp s) `(,(symbol-value s))))
>   (if (boundp s) `(,s ,(symbol-value s)) `(,s))
>
> and I do not find any of these completely satisfactory from the point
> of view of aesthetics and style.  It seems to me that a better (though
> not out-of-the-box) way of doing this would be to extend the `comma'
> read macro to have
>
>   ,?<test-form> <value-form>
>
> be equivalent to
>
>   ,<value-form>
>
> if <test-form> evaluates to true, and to
>
>   ,@nil
>
> otherwise [*].  Thus the above example could be expressed as
>
>   `(,s ,?(boundp s) (symbol-value s))
>
> (I am guessing that introducing such a new meaning for `?' in this
> context will affect few existing programs, if that, and there would be
> an easy workaround in escaping the question mark, if nothing else).

Hmm... how is ,@(when test-form (list value-form)) signifcantly worse
than ,?test-form value-form?

If you really don't want to type a few more characters, you can

(defmacro ? (test-form value-form)
  `(when ,test-form (list ,value-form)))

and then use ,@(? test-form value-form).

Much easier than to modify backquote syntax!

Bye,
Ale
From: Vassil Nikolov
Subject: Re: extending the `comma' read macro (Ex: getting rid of nil)
Date: 
Message-ID: <snzbpnxdu6r.fsf@luna.vassil.nikolov.name>
On Thu, 2 Jul 2009 00:20:39 -0700 (PDT), Alessio Stalla <·············@gmail.com> said:
> ...
> If you really don't want to type a few more characters,

  (It's not about the amount of typing, but the amount of visual
  clutter when reading.)

> you can

> (defmacro ? (test-form value-form)
>   `(when ,test-form (list ,value-form)))

> and then use ,@(? test-form value-form).

> Much easier than to modify backquote syntax!

  You are right that going into backquote syntax may not be worth it
  in this case.  On the other hand, I'd be reluctant to introduce a
  global macro just for this purpose (and then we'd still have a
  little clutter left even with the latter approach).  It seems that
  fundamentally this is a problem with templates which arises when we
  go beyond unconditional, non-iterative substitution of values for
  placeholders.  In the case at hand, I think eventually I'd settle
  for

    (let ((value-part (if (boundp s) `(,(symbol-value s)) '())))
      `(,s ,@value-part))

  ---Vassil.


-- 
"Even when the muse is posting on Usenet, Alexander Sergeevich?"
From: Zach Beane
Subject: Re: getting rid of nil
Date: 
Message-ID: <m363edfwfy.fsf@unnamed.xach.com>
Mirko <·············@gmail.com> writes:

> Is there a way to rewrite
>
>>(let (a)
>    (if a `(5 6 ,a)
>        `(5 6)))
> (5 6)
>
> into
>
>>(let ((a nil))
>    `(5 6 ,(when a a)))
> (5 6 NIL)
> ?
>
> I would like to get rid of the NIL.  I tried, obviously, if.  Anything
> else, or any other approach?

One option is:

  `(5 6 ,@(when a (list a)))

Zach
From: Mirko
Subject: Re: getting rid of nil
Date: 
Message-ID: <471bde8e-4de2-4bf6-971d-f24a963229b6@j19g2000vbp.googlegroups.com>
On Jun 30, 1:44 pm, Zach Beane <····@xach.com> wrote:
> Mirko <·············@gmail.com> writes:
> > Is there a way to rewrite
>
> >>(let (a)
> >    (if a `(5 6 ,a)
> >        `(5 6)))
> > (5 6)
>
> > into
>
> >>(let ((a nil))
> >    `(5 6 ,(when a a)))
> > (5 6 NIL)
> > ?
>
> > I would like to get rid of the NIL.  I tried, obviously, if.  Anything
> > else, or any other approach?
>
> One option is:
>
>   `(5 6 ,@(when a (list a)))
>
> Zach

And (triggered by your idea) here is another

(let ((a nil))
  `(5 ,@(cons 6 (when a (list a)))))

Although yours is better - less consing (If you think I know what
consing is, just because it looks that way, beware).
From: Kenneth Tilton
Subject: Re: getting rid of nil
Date: 
Message-ID: <4a4a7e08$0$5902$607ed4bc@cv.net>
Mirko wrote:
> On Jun 30, 1:44 pm, Zach Beane <····@xach.com> wrote:
>> Mirko <·············@gmail.com> writes:
>>> Is there a way to rewrite
>>>> (let (a)
>>>    (if a `(5 6 ,a)
>>>        `(5 6)))
>>> (5 6)
>>> into
>>>> (let ((a nil))
>>>    `(5 6 ,(when a a)))
>>> (5 6 NIL)
>>> ?
>>> I would like to get rid of the NIL.  I tried, obviously, if.  Anything
>>> else, or any other approach?
>> One option is:
>>
>>   `(5 6 ,@(when a (list a)))
>>
>> Zach
> 
> And (triggered by your idea) here is another
> 
> (let ((a nil))
>   `(5 ,@(cons 6 (when a (list a)))))
> 
> Although yours is better - less consing (If you think I know what
> consing is, just because it looks that way, beware).
> 

We need to work on your trigger. Please eat nothing after 9PM the night 
before.

hth, kt
From: Mirko
Subject: Re: getting rid of nil
Date: 
Message-ID: <17df7e77-41cb-444f-8adb-fc2f62b9df31@j12g2000vbl.googlegroups.com>
On Jun 30, 5:05 pm, Kenneth Tilton <·········@gmail.com> wrote:
> Mirko wrote:
> > On Jun 30, 1:44 pm, Zach Beane <····@xach.com> wrote:
> >> Mirko <·············@gmail.com> writes:
> >>> Is there a way to rewrite
> >>>> (let (a)
> >>>    (if a `(5 6 ,a)
> >>>        `(5 6)))
> >>> (5 6)
> >>> into
> >>>> (let ((a nil))
> >>>    `(5 6 ,(when a a)))
> >>> (5 6 NIL)
> >>> ?
> >>> I would like to get rid of the NIL.  I tried, obviously, if.  Anything
> >>> else, or any other approach?
> >> One option is:
>
> >>   `(5 6 ,@(when a (list a)))
>
> >> Zach
>
> > And (triggered by your idea) here is another
>
> > (let ((a nil))
> >   `(5 ,@(cons 6 (when a (list a)))))
>
> > Although yours is better - less consing (If you think I know what
> > consing is, just because it looks that way, beware).
>
> We need to work on your trigger. Please eat nothing after 9PM the night
> before.
>
> hth, kt

Hey, you were warned: if I sound that I think I know what I am talking
about, that is a warning sign :-)
From: Pascal J. Bourguignon
Subject: Re: getting rid of nil
Date: 
Message-ID: <87ocs5tq0r.fsf@galatea.local>
Mirko <·············@gmail.com> writes:

> On Jun 30, 1:44�pm, Zach Beane <····@xach.com> wrote:
>> Mirko <·············@gmail.com> writes:
>> > Is there a way to rewrite
>>
>> >>(let (a)
>> > � �(if a `(5 6 ,a)
>> > � � � �`(5 6)))
>> > (5 6)
>>
>> > into
>>
>> >>(let ((a nil))
>> > � �`(5 6 ,(when a a)))
>> > (5 6 NIL)
>> > ?
>>
>> > I would like to get rid of the NIL. �I tried, obviously, if. �Anything
>> > else, or any other approach?
>>
>> One option is:
>>
>> � `(5 6 ,@(when a (list a)))
>>
>> Zach
>
> And (triggered by your idea) here is another
>
> (let ((a nil))
>   `(5 ,@(cons 6 (when a (list a)))))
>
> Although yours is better - less consing (If you think I know what
> consing is, just because it looks that way, beware).

 `(5 6 ,@(when a (list a))) is not consing less than 
 `(5 ,@(cons 6 (when a (list a))))

They compile to exactly the same.  But the former is clearer.


C/USER[15]> (disassemble (compile nil (lambda (a)  `(5 6 ,@(when a (list a))))))

Disassembly of function NIL
(CONST 0) = 5
(CONST 1) = 6
1 required argument
0 optional arguments
No rest parameter
No keyword parameters
9 byte-code instructions:
0     (CONST&PUSH 0)                      ; 5
1     (CONST&PUSH 1)                      ; 6
2     (LOAD&JMPIFNOT 3 L8)
5     (PUSH)
6     (LIST 1)
8     L8
8     (CONS)
9     (CONS)
10    (SKIP&RET 2)
NIL
C/USER[16]> (disassemble (compile nil (lambda (a)  `(5 ,@(cons 6 (when a (list a)))))))

Disassembly of function NIL
(CONST 0) = 5
(CONST 1) = 6
1 required argument
0 optional arguments
No rest parameter
No keyword parameters
9 byte-code instructions:
0     (CONST&PUSH 0)                      ; 5
1     (CONST&PUSH 1)                      ; 6
2     (LOAD&JMPIFNOT 3 L8)
5     (PUSH)
6     (LIST 1)
8     L8
8     (CONS)
9     (CONS)
10    (SKIP&RET 2)
NIL
C/USER[17]> 

-- 
__Pascal Bourguignon__
From: Mirko
Subject: Re: getting rid of nil
Date: 
Message-ID: <c7304a02-15d5-487b-a041-6adc9d407d05@g23g2000vbr.googlegroups.com>
On Jun 30, 4:39 pm, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Mirko <·············@gmail.com> writes:
> > On Jun 30, 1:44 pm, Zach Beane <····@xach.com> wrote:
> >> Mirko <·············@gmail.com> writes:
> >> > Is there a way to rewrite
>
> >> >>(let (a)
> >> > (if a `(5 6 ,a)
> >> > `(5 6)))
> >> > (5 6)
>
> >> > into
>
> >> >>(let ((a nil))
> >> > `(5 6 ,(when a a)))
> >> > (5 6 NIL)
> >> > ?
>
> >> > I would like to get rid of the NIL. I tried, obviously, if. Anything
> >> > else, or any other approach?
>
> >> One option is:
>
> >> `(5 6 ,@(when a (list a)))
>
> >> Zach
>
> > And (triggered by your idea) here is another
>
> > (let ((a nil))
> >   `(5 ,@(cons 6 (when a (list a)))))
>
> > Although yours is better - less consing (If you think I know what
> > consing is, just because it looks that way, beware).
>
>  `(5 6 ,@(when a (list a))) is not consing less than
>  `(5 ,@(cons 6 (when a (list a))))
>
> They compile to exactly the same.  But the former is clearer.
>
> C/USER[15]> (disassemble (compile nil (lambda (a)  `(5 6 ,@(when a (list a))))))
>
> Disassembly of function NIL
> (CONST 0) = 5
> (CONST 1) = 6
> 1 required argument
> 0 optional arguments
> No rest parameter
> No keyword parameters
> 9 byte-code instructions:
> 0     (CONST&PUSH 0)                      ; 5
> 1     (CONST&PUSH 1)                      ; 6
> 2     (LOAD&JMPIFNOT 3 L8)
> 5     (PUSH)
> 6     (LIST 1)
> 8     L8
> 8     (CONS)
> 9     (CONS)
> 10    (SKIP&RET 2)
> NIL
> C/USER[16]> (disassemble (compile nil (lambda (a)  `(5 ,@(cons 6 (when a (list a)))))))
>
> Disassembly of function NIL
> (CONST 0) = 5
> (CONST 1) = 6
> 1 required argument
> 0 optional arguments
> No rest parameter
> No keyword parameters
> 9 byte-code instructions:
> 0     (CONST&PUSH 0)                      ; 5
> 1     (CONST&PUSH 1)                      ; 6
> 2     (LOAD&JMPIFNOT 3 L8)
> 5     (PUSH)
> 6     (LIST 1)
> 8     L8
> 8     (CONS)
> 9     (CONS)
> 10    (SKIP&RET 2)
> NIL
> C/USER[17]>
>
> --
> __Pascal Bourguignon__

You're right - I get identical compilatios on  SBCL as well.
From: vk00
Subject: Re: getting rid of nil
Date: 
Message-ID: <90a6df09-4d7f-4b34-9dcc-403718ed4d43@f30g2000vbf.googlegroups.com>
On Jun 30, 1:40 pm, Mirko <·············@gmail.com> wrote:
> Is there a way to rewrite
>
> >(let (a)
>
>    (if a `(5 6 ,a)
>        `(5 6)))
> (5 6)
>
> into
>
> >(let ((a nil))
>
>    `(5 6 ,(when a a)))
> (5 6 NIL)
> ?
>
> I would like to get rid of the NIL.  I tried, obviously, if.  Anything
> else, or any other approach?
>
> Thank you
>
> Mirko

what about an anaphoric macro:
(defmacro aif (test-form then-form &optional else-form)
  `(let ((it ,test-form))
     (if it ,then-form ,else-form)))

(aif a
  `(5 6 ,it)
  `(5 6))

if a is false you get (5 6), otherwise (5 6 ,it) where it is bound to
a.

works in sbcl at least.

-vk