Volkan YAZICI <·············@gmail.com> writes:
> Hi,
>
> Shouldn't (format nil "~{~:[NOT NULL~;~]·@[ DEFAULT ~a~]~^, ~}"
> '((nil 12) (t nil)))
> be outputting "NOT NULL DEFAULT 12, " instead of
> "DEFAULT (T NIL)"? (Using SBCL 1.0.19.27)
No. It is acting correctly.
That is because the first argument passed to the ~:[ is (nil 12) and not
the nil that you expect.
To do what you want to do, you need another level of ~{...~}:
(format nil "~{~{~:[NOT NULL~;~]·@[ DEFAULT ~a~]~}~^, ~}"
'((nil 12) (t nil)))
> Regards.
>
> P.S. BTW, is "22.3.7.2 Tilde Left-Bracket: Conditional Expression"
> poorly documented (e.g. no explanation for `#' directive, but just an
> example) or it's just me?
No. The standard tends to be a bit terse, but # is not a directive, but
is instead a standard part of the format syntax that can be used in the
place where normally one would have a parameter. It is documented
earlier in the text in section 22.3:
In place of a prefix parameter to a directive, V (or v) can be
used. In this case, format takes an argument from args as a parameter
to the directive. The argument should be an integer or character. If
the arg used by a V parameter is nil, the effect is as if the
parameter had been omitted. # can be used in place of a prefix
parameter; it represents the number of args remaining to be
processed. When used within a recursive format, in the context of ~?
or ~{, the # prefix parameter represents the number of format
arguments remaining within the recursive call.
So, this is just pointing out a good use of # to pass the number of
remaining arguments as a parameter to the ~[ directive.
--
Thomas A. Russ, USC/Information Sciences Institute
On Sep 18, 7:43 pm, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> Volkan YAZICI <·············@gmail.com> writes:
> > Shouldn't (format nil "~{~:[NOT NULL~;~]·@[ DEFAULT ~a~]~^, ~}"
> > '((nil 12) (t nil)))
> > be outputting "NOT NULL DEFAULT 12, " instead of
> > "DEFAULT (T NIL)"? (Using SBCL 1.0.19.27)
>
> No. It is acting correctly.
> That is because the first argument passed to the ~:[ is (nil 12) and not
> the nil that you expect.
>
> To do what you want to do, you need another level of ~{...~}:
>
> (format nil "~{~{~:[NOT NULL~;~]·@[ DEFAULT ~a~]~}~^, ~}"
> '((nil 12) (t nil)))
>
> > Regards.
>
> > P.S. BTW, is "22.3.7.2 Tilde Left-Bracket: Conditional Expression"
> > poorly documented (e.g. no explanation for `#' directive, but just an
> > example) or it's just me?
>
> No. The standard tends to be a bit terse, but # is not a directive, but
> is instead a standard part of the format syntax that can be used in the
> place where normally one would have a parameter. It is documented
> earlier in the text in section 22.3:
>
> In place of a prefix parameter to a directive, V (or v) can be
> used. In this case, format takes an argument from args as a parameter
> to the directive. The argument should be an integer or character. If
> the arg used by a V parameter is nil, the effect is as if the
> parameter had been omitted. # can be used in place of a prefix
> parameter; it represents the number of args remaining to be
> processed. When used within a recursive format, in the context of ~?
> or ~{, the # prefix parameter represents the number of format
> arguments remaining within the recursive call.
>
> So, this is just pointing out a good use of # to pass the number of
> remaining arguments as a parameter to the ~[ directive.
My bad. Sorry for the noise and thanks for the detailed reply.
Regards.
It is perhaps a better practice to pump the conditionals out of the
format statements:
(if (null condition)
(format nil "condition is null")
(format nil "condition is true"))
That way, you have all the power of the language.
From: Pascal J. Bourguignon
Subject: Re: FORMAT Conditionals Weirdness
Date:
Message-ID: <7c8wtoqkwt.fsf@anevia.com>
·······@eurogaran.com writes:
> It is perhaps a better practice to pump the conditionals out of the
> format statements:
> (if (null condition)
> (format nil "condition is null")
> (format nil "condition is true"))
>
> That way, you have all the power of the language.
or:
(format nil "condition is ~A" (if (null condition) "NIL" "true"))
--
__Pascal Bourguignon__