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
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
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
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
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)))
?
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
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))
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?"
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
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?"
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
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).
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
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 :-)
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