[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

FORMAT: Non-consuming conditional

Sebastian Christ

1/3/2016 7:26:00 PM

Hello c.l.l,

I'm stuck with my control string. What I want is:

(format t CONTROL-STRING nil "foo") ;=> No Scope - Name: foo
(format t CONTROL-STRING "baz" "foo") ;=> Scope: baz - Name: foo

What I've tried was

(format t "~:[No Scope~:Scope: ~A~] - Name: ~A" scope name)

but then the conditional consumes SCOPE, uses NAME for "Scope: ~A" and
then there is no argument left for the "Name: ~A" part. Other
experiments with @ and # modifiers were unsuccessful as well. :(

Perhaps there is a simpler solution, but I'm not seeing it yet.

Thanks in advance.

Regards,
Sebastian

--
Sebastian (Rudolfo) Christ
http://rudolfochrist...
GPG Fingerprint: 306D 8FD3 DFB6 4E44 5061
CE71 6407 D6F8 2AC5 55DD
44 Answers

Kalle Olavi Niemitalo

1/3/2016 7:53:00 PM

0

Sebastian Christ <rudolfo.christ@gmail.com> writes:

> but then the conditional consumes SCOPE, uses NAME for "Scope: ~A" and
> then there is no argument left for the "Name: ~A" part. Other
> experiments with @ and # modifiers were unsuccessful as well. :(

You can un-consume SCOPE with "~:*":
(format t "~:[No Scope~;Scope: ~:*~A~] - Name: ~A" scope name)

Ben Bacarisse

1/3/2016 8:37:00 PM

0

Sebastian Christ <rudolfo.christ@gmail.com> writes:

> Hello c.l.l,
>
> I'm stuck with my control string. What I want is:
>
> (format t CONTROL-STRING nil "foo") ;=> No Scope - Name: foo
> (format t CONTROL-STRING "baz" "foo") ;=> Scope: baz - Name: foo
>
> What I've tried was
>
> (format t "~:[No Scope~:Scope: ~A~] - Name: ~A" scope name)
>
> but then the conditional consumes SCOPE, uses NAME for "Scope: ~A" and
> then there is no argument left for the "Name: ~A" part. Other
> experiments with @ and # modifiers were unsuccessful as well. :(

Well,

> (format t fmt nil "foo")
No Scope - Name: foo
NIL
> (format t fmt "baz" "foo")
Scope: baz - Name: foo
NIL
> fmt
"~@[Scope: ~A - Name: ~A~]~#[~;No Scope - Name: ~A~]"

but that's rather yucky.

--
Ben.

Ben Bacarisse

1/3/2016 8:39:00 PM

0

Kalle Olavi Niemitalo <kon@iki.fi> writes:

> Sebastian Christ <rudolfo.christ@gmail.com> writes:
>
>> but then the conditional consumes SCOPE, uses NAME for "Scope: ~A" and
>> then there is no argument left for the "Name: ~A" part. Other
>> experiments with @ and # modifiers were unsuccessful as well. :(
>
> You can un-consume SCOPE with "~:*":
> (format t "~:[No Scope~;Scope: ~:*~A~] - Name: ~A" scope name)

Much nicer than mine.

--
Ben.

William James

1/4/2016 12:03:00 AM

0

Sebastian Christ wrote:

> Hello c.l.l,
>
> I'm stuck with my control string. What I want is:
>
> (format t CONTROL-STRING nil "foo") ;=> No Scope - Name: foo
> (format t CONTROL-STRING "baz" "foo") ;=> Scope: baz - Name: foo
>
> What I've tried was
>
> (format t "~:[No Scope~:Scope: ~A~] - Name: ~A" scope name)
>
> but then the conditional consumes SCOPE, uses NAME for "Scope: ~A" and
> then there is no argument left for the "Name: ~A" part. Other
> experiments with @ and # modifiers were unsuccessful as well. :(

MatzLisp (Ruby):

"#{scope ? "Scope: #{scope}" : "No scope"} - Name: #{name}"

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...

Kaz Kylheku

1/4/2016 4:34:00 AM

0

On 2016-01-03, Sebastian Christ <rudolfo.christ@gmail.com> wrote:
> Hello c.l.l,
>
> I'm stuck with my control string. What I want is:
>
> (format t CONTROL-STRING nil "foo") ;=> No Scope - Name: foo
> (format t CONTROL-STRING "baz" "foo") ;=> Scope: baz - Name: foo
>
> What I've tried was
>
> (format t "~:[No Scope~:Scope: ~A~] - Name: ~A" scope name)

> but then the conditional consumes SCOPE, uses NAME for "Scope: ~A" and
> then there is no argument left for the "Name: ~A" part. Other
> experiments with @ and # modifiers were unsuccessful as well. :(
>
> Perhaps there is a simpler solution, but I'm not seeing it yet.

There is always KISS:

(defun scope-str (scope)
(if scope
(format nil "Scope: ~a" scope)
"No Scope"))

(format t "~a - Name ~a" (scope-str scope) name)

Don't like the accumulation of a string? We can easily turn this into a
function usable with format's tilde slash:

(defun fmt-scope (stream scope at-p colon-p)
(if scope
(format stream "Scope: ~a" scope)
(write-string "No Scope" stream)))

Usage:

[2]> (format t "~/fmt-scope/ - Name ~a~%" 'foo 'foo)
Scope: FOO - Name FOO
NIL
[3]> (format t "~/fmt-scope/ - Name ~a~%" nil 'foo)
No Scope - Name FOO

The above will still be obvious at a glance 17 months after
*not* looking at it.

Pascal J. Bourguignon

1/4/2016 5:37:00 AM

0

Kaz Kylheku <kaz@kylheku.com> writes:

> On 2016-01-03, Sebastian Christ <rudolfo.christ@gmail.com> wrote:
>> Hello c.l.l,
>>
>> I'm stuck with my control string. What I want is:
>>
>> (format t CONTROL-STRING nil "foo") ;=> No Scope - Name: foo
>> (format t CONTROL-STRING "baz" "foo") ;=> Scope: baz - Name: foo
>>
>> What I've tried was
>>
>> (format t "~:[No Scope~:Scope: ~A~] - Name: ~A" scope name)
>
>> but then the conditional consumes SCOPE, uses NAME for "Scope: ~A" and
>> then there is no argument left for the "Name: ~A" part. Other
>> experiments with @ and # modifiers were unsuccessful as well. :(
>>
>> Perhaps there is a simpler solution, but I'm not seeing it yet.
>
> There is always KISS:
>
> (defun scope-str (scope)
> (if scope
> (format nil "Scope: ~a" scope)
> "No Scope"))
>
> (format t "~a - Name ~a" (scope-str scope) name)
>
> Don't like the accumulation of a string? We can easily turn this into a
> function usable with format's tilde slash:
>
> (defun fmt-scope (stream scope at-p colon-p)
> (if scope
> (format stream "Scope: ~a" scope)
> (write-string "No Scope" stream)))
>
> Usage:
>
> [2]> (format t "~/fmt-scope/ - Name ~a~%" 'foo 'foo)
> Scope: FOO - Name FOO
> NIL
> [3]> (format t "~/fmt-scope/ - Name ~a~%" nil 'foo)
> No Scope - Name FOO
>
> The above will still be obvious at a glance 17 months after
> *not* looking at it.


What is not obvious in:

(loop for foo in '(nil foo)
do (format t "~:[No Scope~;~:*Scope: ~A~] - Name ~A~%" foo 'bar))
No Scope - Name bar
Scope: foo - Name bar

?


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Sebastian Christ

1/4/2016 8:55:00 AM

0

Thank you. This is what I was thinking of.

Regards,
Sebastian

--
Sebastian (Rudolfo) Christ
http://rudolfochrist...
GPG Fingerprint: 306D 8FD3 DFB6 4E44 5061
CE71 6407 D6F8 2AC5 55DD

Sebastian Christ

1/4/2016 8:59:00 AM

0

>>>>> On Mon, 4 Jan 2016 04:33:34 +0000 (UTC), Kaz Kylheku <kaz@kylheku.com> said:
> There is always KISS:
>
> (defun scope-str (scope)
> (if scope
> (format nil "Scope: ~a" scope)
> "No Scope"))
>
> (format t "~a - Name ~a" (scope-str scope) name)

I wanted to keep this as a last resort.

> Don't like the accumulation of a string? We can easily turn this into a
> function usable with format's tilde slash:
>
> (defun fmt-scope (stream scope at-p colon-p)
> (if scope
> (format stream "Scope: ~a" scope)
> (write-string "No Scope" stream)))
>
> Usage:
>
> [2]> (format t "~/fmt-scope/ - Name ~a~%" 'foo 'foo)
> Scope: FOO - Name FOO
> NIL
> [3]> (format t "~/fmt-scope/ - Name ~a~%" nil 'foo)
> No Scope - Name FOO
>
> The above will still be obvious at a glance 17 months after
> *not* looking at it.

This is cool. Thank you for pointing me to this. I think I will need
this in the future.

Regards,
Sebastian

--
Sebastian (Rudolfo) Christ
http://rudolfochrist...
GPG Fingerprint: 306D 8FD3 DFB6 4E44 5061
CE71 6407 D6F8 2AC5 55DD

Kenneth Tilton

1/4/2016 2:04:00 PM

0

On Sunday, January 3, 2016 at 2:26:39 PM UTC-5, Sebastian Christ wrote:
> Hello c.l.l,
>
> I'm stuck with my control string. What I want is:
>
> (format t CONTROL-STRING nil "foo") ;=> No Scope - Name: foo
> (format t CONTROL-STRING "baz" "foo") ;=> Scope: baz - Name: foo
>
> What I've tried was
>
> (format t "~:[No Scope~:Scope: ~A~] - Name: ~A" scope name)
>
> but then the conditional consumes SCOPE, uses NAME for "Scope: ~A" and
> then there is no argument left for the "Name: ~A" part. Other
> experiments with @ and # modifiers were unsuccessful as well. :(
>
> Perhaps there is a simpler solution, but I'm not seeing it yet.
>
> Thanks in advance.
>
> Regards,
> Sebastian
>
> --
> Sebastian (Rudolfo) Christ
> http://rudolfochrist...
> GPG Fingerprint: 306D 8FD3 DFB6 4E44 5061
> CE71 6407 D6F8 2AC5 55DD

I am not saying the CLHS is easy to read, but you have a very clear idea of what is going wrong so I think the solution will jump out at you if you peruse this: http://www.lispworks.com/documentation/lw50/CLHS/Body/...

hth.

-hk

ps. I know someone already gave you the fish, I thought you might like to know where you can find your own. -h

Sebastian Christ

1/4/2016 2:23:00 PM

0

Hi Kenny,

THIS IS EMBARRASSING.

I thought I've read the whole page. Sigh.

Thank you for not only showing me where I can find the fish, but also
that it takes time, concentration and thinking to catch it.

Regards,
Sebastian

--
Sebastian (Rudolfo) Christ
http://rudolfochrist...
GPG Fingerprint: 306D 8FD3 DFB6 4E44 5061
CE71 6407 D6F8 2AC5 55DD