From: Shannon Lloyd
Subject: outputting a string without quotes
Date: 
Message-ID: <Uksr9.27768$334.66301@news-server.bigpond.net.au>
How can I return a string (to eventually be printed to the console) without
printing the quotation marks? I'm using (format nil .....) to return the
string to the calling function, because I don't want to print the string to
the console immediately or have it return nil (ie with format t), and I'd
like to be able to display the string as the final step without quotes
around it. Any ideas?
Thanks,
Shannon

From: Kenny Tilton
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <O7tr9.10605$gB.3383127@twister.nyc.rr.com>
Shannon Lloyd wrote in message ...
>How can I return a string (to eventually be printed to the console) without
>printing the quotation marks? I'm using (format nil .....) to return the
>string to the calling function, because I don't want to print the string to
>the console immediately or have it return nil (ie with format t), and I'd
>like to be able to display the string as the final step without quotes
>around it. Any ideas?

-d? not when you return it, but whenever you get around to printing it to
the console. that's if you think you'll onle ever need thus to manage
strings.

> (format t "~d" "hi mom")
hi mom
NIL
> (format t "~s" "hi mom")
"hi mom"
NIL
>

or, if you think this is going to be a major/frequent issue, define a
wrapper class with all sorts of expressive power, then return an instance of
that. Arrange for it to print out neatly (see above) by specializing
print-object on the wrapper class.

k,c
From: Shannon Lloyd
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <Aotr9.28201$334.66962@news-server.bigpond.net.au>
"Kenny Tilton" <·······@nyc.rr.com> wrote in message
···························@twister.nyc.rr.com...
> Shannon Lloyd wrote in message ...
> >How can I return a string (to eventually be printed to the console)
without
> >printing the quotation marks? I'm using (format nil .....) to return the
> >string to the calling function, because I don't want to print the string
to
> >the console immediately or have it return nil (ie with format t), and I'd
> >like to be able to display the string as the final step without quotes
> >around it. Any ideas?
>
> -d? not when you return it, but whenever you get around to printing it to
> the console. that's if you think you'll onle ever need thus to manage
> strings.
>
> > (format t "~d" "hi mom")
> hi mom
> NIL
> > (format t "~s" "hi mom")
> "hi mom"
> NIL
> >
>
> or, if you think this is going to be a major/frequent issue, define a
> wrapper class with all sorts of expressive power, then return an instance
of
> that. Arrange for it to print out neatly (see above) by specializing
> print-object on the wrapper class.
>
> k,c
>
>

thanks for the reply, but i was actually hoping to get just the unquoted
string, not the returned "nil" after it, which was why i mentioned that i
was using the 'nil' option for format rather than the 't' option. it seems
that i can have one or the other, but i can't get it to return just the
unquoted string. perhaps i can, but i'm having a hell of a time finding out
how.
shannon
From: Fred Gilham
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <u77kgh3uu9.fsf@snapdragon.csl.sri.com>
> thanks for the reply, but i was actually hoping to get just the
> unquoted string, not the returned "nil" after it, which was why i
> mentioned that i was using the 'nil' option for format rather than
> the 't' option. it seems that i can have one or the other, but i
> can't get it to return just the unquoted string. perhaps i can, but
> i'm having a hell of a time finding out how.

There are several areas of confusion here.

1) If you want to do direct printout, you want something like this.

(progn
  (format t "~A~%" "hi mom")
  (values)) ; This is explained further down.

The second argument to format, in this case "t", is the "stream" that
format writes its output to.  Except if it is t, then format uses
*standard-output*, and if it is nil, format returns its output as a
string instead of writing it to an output stream.

2) In Lisp everything returns a value (except for things that don't).

So format will return a value.

The little code snippet above, however, uses progn to return the value
of the last form (which in this case is the second form, since there
are only two).  This form is the form (values), which is part of the
second class of things mentioned above, those that don't return any
values.  So (values) is used when you definitely don't want to return
a value.

I should explain that (values) is used to return 0 or more values.  So
you can do, for example, (values 1 2 3 4) if you want to return
multiple values instead of just one (or none).  Without (values), most
Lisp forms are like the functions we're used to --- they return a
single value.

Note that most printing is done in the context of a program.  Your
program should use format as above, and not worry about what it
returns.  Lisp will print the return value of your program, not that
of the format statement (unless format's return value IS the return
value of your program).

For example, I do

(defun print-some-numbers (n)
  (dotimes (i n)
    (format t "~D~%" (1+ i))))

* (print-some-numbers 5)
1
2
3
4
5
NIL
* 

The nil is what my program returns as its return value.  Kind of like
a C program having a return code when it exits, except more general.

If I don't like the nil at the end, I can do one of the following:

(defun print-some-numbers (n)
  (dotimes (i n)
    (format t "~D~%" (1+ i)))
  (values))  ; My program won't return anything.

* (print-some-numbers 5)
1
2
3
4
5
* 

Or, noting that the result of the dotimes form is what my program
returns, I can make dotimes return no values:

(defun print-some-numbers (n)
  (dotimes (i n (values)) ; Make dotimes return no values.
    (format t "~D~%" (1+ i))))

* (print-some-numbers 5)
1
2
3
4
5
* 

Hope this helps.

-- 
Fred Gilham                                      ······@csl.sri.com
Ah, the 20th century, when the flight from reason crash-landed into
the slaughterhouse.  --- James Ostrowski
From: Adam Warner
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <pan.2002.10.17.07.39.05.142857@consulting.net.nz>
Hi Shannon Lloyd,

> thanks for the reply, but i was actually hoping to get just the unquoted
> string, not the returned "nil" after it, which was why i mentioned that i
> was using the 'nil' option for format rather than the 't' option. it seems
> that i can have one or the other, but i can't get it to return just the
> unquoted string. perhaps i can, but i'm having a hell of a time finding out
> how.
> shannon

Shannon, in Lisp everything has a return value. What is displayed depends
upon the implementation.

Here's CLISP:
[3]> (format nil "string")
"string"
[4]> 

Here's CMUCL:
* (format nil "string")
                         <-- no printed output
"string"
*

CMUCL is showing a blank line when nothing has been printed.

You can use princ or format's ~A (aesthetic) to print unescaped and
unquoted text:

[4]> (princ "\\backslash")
\backslash
"\\backslash"
[5]> (format t "~A" "\\backslash")
\backslash
nil

Note that in the first case princ printed and returned the string. In the
second case format printed the string and returned nil.

Do not worry for a moment that the interactive environment showed you what
your program printed and the return value of the function call. Just
understand why you got two lines of output.

Regards,
Adam
From: Christopher C. Stacy
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <uy98xbl40.fsf@dtpq.com>
>>>>> On Thu, 17 Oct 2002 05:59:16 GMT, Shannon Lloyd ("Shannon") writes:

 Shannon> How can I return a string (to eventually be printed to the console)
 Shannon> without printing the quotation marks? I'm using (format nil .....)
 Shannon> to return the string to the calling function, because I don't want
 Shannon> to print the string to the console immediately or have it return
 Shannon> nil (ie with format t), and I'd like to be able to display the
 Shannon> string as the final step without quotes around it. Any ideas?


When you invoke a function from the Lisp listener (the interactive
loop that does READ-EVAL-PRINT) it prints the results of the ultimate
evaluation.  You seem to know that.

If your program needs to return a value, and that value is a string,
then you have no choice about the R-E-P loop printing it out.
The job of R-E-P is to print out the value in a way that would be
visually suitable for supplying as input to another READ.

So, if you had this function:

  (defun foo ()
    "the answer")

and invoked it using R-E-P, the result would be "the answer".
It's going to display the result with the quotes, because in
order for you to give that value to a call to READ, you would
need to type the quotes:

> (READ)
"something"
--> "something"
>

However, if the final step of the program is to actually print
something on the console -- as part of what the function should do --
then you should call one of the output functions: PRINT.  PRIN1, 
PRINC, WRITE, or FORMAT.  Let's assume FORMAT since you're 
familiar with it.

  (defun foo ()
    (let ((a "the answer"))
      (format t "~&~A~%" a)
      a))

The above program will compute the answer, print it out, and then also
return the answer (presumably for consumption by some calling program).

When you invoke it from R-E-P, it's going to look something like this:

> (foo)
the answer

"the answer"
>

If you want to supress the R-E-P's display of the value,
you will have to call some other function that does not, 
in fact, return that value!

(defun bar ()
  (foo)        ;for side-effect (output) only
  nil)         ;don't return value of that call, return NIL instead

> (bar)
the answer

NIL
>

Now, here comes the trick that perhaps you're looking for.  
Just as it is possible for a function to return more than one 
value (something we have not done here - see MULTIPLE-VALUE-BIND 
in the documentation), it is also possible to return "no value"!

(defun bar ()
  (foo)        ;for side-effect (output) only
  (values))    ;return no values at all from BAR

> (bar)
the answer

>

Hope that clears it up!
Chris
From: Shannon Lloyd
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <Sbur9.28328$334.67486@news-server.bigpond.net.au>
"Christopher C. Stacy" <······@dtpq.com> wrote in message
··················@dtpq.com...
> >>>>> On Thu, 17 Oct 2002 05:59:16 GMT, Shannon Lloyd ("Shannon") writes:
>
>  Shannon> How can I return a string (to eventually be printed to the
console)
>  Shannon> without printing the quotation marks? I'm using (format nil
.....)
>  Shannon> to return the string to the calling function, because I don't
want
>  Shannon> to print the string to the console immediately or have it return
>  Shannon> nil (ie with format t), and I'd like to be able to display the
>  Shannon> string as the final step without quotes around it. Any ideas?
>
>
> When you invoke a function from the Lisp listener (the interactive
> loop that does READ-EVAL-PRINT) it prints the results of the ultimate
> evaluation.  You seem to know that.
>
> If your program needs to return a value, and that value is a string,
> then you have no choice about the R-E-P loop printing it out.
> The job of R-E-P is to print out the value in a way that would be
> visually suitable for supplying as input to another READ.
>
> So, if you had this function:
>
>   (defun foo ()
>     "the answer")
>
> and invoked it using R-E-P, the result would be "the answer".
> It's going to display the result with the quotes, because in
> order for you to give that value to a call to READ, you would
> need to type the quotes:
>
> > (READ)
> "something"
> --> "something"
> >
>
> However, if the final step of the program is to actually print
> something on the console -- as part of what the function should do --
> then you should call one of the output functions: PRINT.  PRIN1,
> PRINC, WRITE, or FORMAT.  Let's assume FORMAT since you're
> familiar with it.
>
>   (defun foo ()
>     (let ((a "the answer"))
>       (format t "~&~A~%" a)
>       a))
>
> The above program will compute the answer, print it out, and then also
> return the answer (presumably for consumption by some calling program).
>
> When you invoke it from R-E-P, it's going to look something like this:
>
> > (foo)
> the answer
>
> "the answer"
> >
>
> If you want to supress the R-E-P's display of the value,
> you will have to call some other function that does not,
> in fact, return that value!
>
> (defun bar ()
>   (foo)        ;for side-effect (output) only
>   nil)         ;don't return value of that call, return NIL instead
>
> > (bar)
> the answer
>
> NIL
> >
>
> Now, here comes the trick that perhaps you're looking for.
> Just as it is possible for a function to return more than one
> value (something we have not done here - see MULTIPLE-VALUE-BIND
> in the documentation), it is also possible to return "no value"!
>
> (defun bar ()
>   (foo)        ;for side-effect (output) only
>   (values))    ;return no values at all from BAR
>
> > (bar)
> the answer
>
> >
>
> Hope that clears it up!
> Chris
>

cheers chris, this is exactly what i was looking for.thanks for your help.
shannon
From: Raymond Wiker
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <86of9ty012.fsf@raw.grenland.fast.no>
"Shannon Lloyd" <·········@SPAMlikwid.com> writes:

> How can I return a string (to eventually be printed to the console) without
> printing the quotation marks? I'm using (format nil .....) to return the
> string to the calling function, because I don't want to print the string to
> the console immediately or have it return nil (ie with format t), and I'd
> like to be able to display the string as the final step without quotes
> around it. Any ideas?

    I assume that the string-in-quotes is what is being printed
by the Lisp top-level. In other words, it's the result returned from a
form called interactively. One way of achieving what you want is
to explicitly print the string (using format, probably), and forcing
the top-level form to return nothing.

(progn
   (let ((my-string (format nil "hello, world!")))
       (format t "~a~%" my-string))
   (values))  ;;; no return value!


-- 
Raymond Wiker                        Mail:  ·············@fast.no
Senior Software Engineer             Web:   http://www.fast.no/
Fast Search & Transfer ASA           Phone: +47 23 01 11 60
P.O. Box 1677 Vika                   Fax:   +47 35 54 87 99
NO-0120 Oslo, NORWAY                 Mob:   +47 48 01 11 60

Try FAST Search: http://alltheweb.com/
From: Adam Warner
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <pan.2002.10.17.09.12.32.391256@consulting.net.nz>
Hi Raymond Wiker,

> (progn
>    (let ((my-string (format nil "hello, world!")))
>        (format t "~a~%" my-string))
>    (values))  ;;; no return value!

That's fascinating. (values) doesn't even return nil! Is this the only
exception to built in Lisp functions/macros etc. returning a value?

Anyone care to explain why we still get NIL as the output from format just
like (values) returned NIL:

* (format t "~A" (values))
NIL
NIL
* (format t "~A" nil)        
NIL
NIL

Regards,
Adam
From: Johannes Grødem
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <lzd6q98lcg.fsf@unity.copyleft.no>
* "Adam Warner" <······@consulting.net.nz>:

> Anyone care to explain why we still get NIL as the output from format just
> like (values) returned NIL:

If you're using the result of an expression[1] somewhere, you will get
nil if the expression doesn't return a value.  I think this is in the
standard somewhere, but I couldn't find it right now.


---
1. Should I say "form" here?

-- 
Johannes Gr�dem <OpenPGP: 5055654C>
From: Kalle Olavi Niemitalo
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <877kg5ku4a.fsf@Astalo.y2000.kon.iki.fi>
"Johannes Gr�dem" <······@ifi.uio.no> writes:

> If you're using the result of an expression[1] somewhere, you will get
> nil if the expression doesn't return a value.  I think this is in the
> standard somewhere, but I couldn't find it right now.

It is at 3.1.7 Return Values.
From: Raymond Wiker
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <86k7khxukd.fsf@raw.grenland.fast.no>
"Adam Warner" <······@consulting.net.nz> writes:

> Hi Raymond Wiker,
> 
> > (progn
> >    (let ((my-string (format nil "hello, world!")))
> >        (format t "~a~%" my-string))
> >    (values))  ;;; no return value!
> 
> That's fascinating. (values) doesn't even return nil! Is this the only
> exception to built in Lisp functions/macros etc. returning a value?
> 
> Anyone care to explain why we still get NIL as the output from format just
> like (values) returned NIL:

        (values) does _not_ return nil. If you try to use more results
values than are actually returned, you will get nil in those places:

(multiple-value-bind (a b c) (values)
        (format t "a=~a, b=~a, c=~a~%" a b c))


-- 
Raymond Wiker                        Mail:  ·············@fast.no
Senior Software Engineer             Web:   http://www.fast.no/
Fast Search & Transfer ASA           Phone: +47 23 01 11 60
P.O. Box 1677 Vika                   Fax:   +47 35 54 87 99
NO-0120 Oslo, NORWAY                 Mob:   +47 48 01 11 60

Try FAST Search: http://alltheweb.com/
From: Frode Vatvedt Fjeld
Subject: Re: outputting a string without quotes
Date: 
Message-ID: <2hvg41z7mm.fsf@vserver.cs.uit.no>
"Adam Warner" <······@consulting.net.nz> writes:

> That's fascinating. (values) doesn't even return nil! Is this the
> only exception to built in Lisp functions/macros etc. returning a
> value?

No, pprint (at least) also returns no values, and obviously many
functions return more than one value.

> Anyone care to explain why we still get NIL as the output from
> format just like (values) returned NIL:

When a function is called, each of its argument-forms are evaluated
for their primary value. That is, no argument-form can "disappear"
from the list of arguments by returning zero values, exactly one value
is produced by each argument-form. This is one of the most basic rules
of evaluation in Common Lisp.

In CL, whenever fewer values are provided than expected, the value NIL
is used. For example,

  (nth-value 0 (values)) => nil
  (nth-value 2 (values 0 1 2 3)) => 2
  (nth-value 100 (values 0 1 2 3)  => nil


Look up multiple-value-call for a mechanism that takes a different
approach to evaluating functions' argument-forms.

-- 
Frode Vatvedt Fjeld