From: Fred
Subject: printing help
Date: 
Message-ID: <yWun4.544$Xf3.6242@typhoon.nyroc.rr.com>
Hello.

I am a LISP newbie, and I have a few general printing questions.

1. I have a function that could take a while to run, and I would like a
message to appear with an indicator such as "please wait", and disappear
when the rest of the output is displayed.
For example, while the program is running:

Input: blah blah
Goal: blah blah blah
Please wait . . .

And when it is finished I would like to write over the Please wait:

Input: blah blah
Goal: blah blah blah
Result: add one blah to the end

I was thinking Carriage return or x amount of backspaces? (both of which I
don't know how to do.)

2. Lisp always returns a value. Is there a way to prevent from displaying
it?
For example, suppose I want to display something After the answer in a
function.
(defun sum (num1 num2)
    (princ (+ num1 num2)) (princ " is the sum.")(terpri)
)

->(sum 3 4)
7 is the sum.
NIL
->

How do I not show the NIL?

TIA.

--
Fred Marcus

To Reply, remove NOSPAM from my email address.

From: Espen Vestre
Subject: Re: printing help
Date: 
Message-ID: <w6900xcsvc.fsf@wallace.nextel.no>
"Fred" <········@NOSPAMrochester.rr.com> writes:

> I was thinking Carriage return or x amount of backspaces? (both of which I
> don't know how to do.)

Look at your lisp documentation to see how you can designate special
characters with the #\-syntax.

Whether or not this will work for you too:

 USER(2): (progn 
            (format t "please wait...")
            (sleep 2)
            (format t "~aNow I'm done~%" #\return))
 Now I'm done..
 NIL
 USER(3): 

of course depends on the environment you're running it in (e.g. it will
work as you desire with my lisp from the unix command line, but not 
inside emacs).

> 2. Lisp always returns a value. 

This is not true.  Lisp can return from 0 to n values.  See the
documentation of VALUES.

-- 
  (espen)
From: Christopher J. Vogt
Subject: Re: printing help
Date: 
Message-ID: <389EFA93.68C80322@computer.org>
Fred wrote:
> [ ... ]
>
> 2. Lisp always returns a value. Is there a way to prevent from displaying
> it?
> For example, suppose I want to display something After the answer in a
> function.
> (defun sum (num1 num2)
>     (princ (+ num1 num2)) (princ " is the sum.")(terpri)
> )
> 
> ->(sum 3 4)
> 7 is the sum.
> NIL
> ->
> 
> How do I not show the NIL?

Actually, Lisp always returns 0 or more values.  Here is what I think you want:

(defun sum (num1 num2)
  (princ (+ num1 num2))
  (princ " is the sum.")
  (terpri)
  (values))

->(sum 3 4)
7 is the sum.
->
From: Robert Munyer
Subject: Re: printing help
Date: 
Message-ID: <87qp2v$g5j$1@eve.enteract.com>
Other people have already answered your specific questions, so I'll
just give you some general advice.

You are trying to make your functions do "funky things" with the
read-eval-print loop.  That's fine if you're doing it for fun, or
for a specific project that needs it.  But you should be aware that
this is not one of the most important aspects of Lisp, and focusing
on this kind of stuff is not the best approach if you're just trying
to become a skilled Lisp programmer.

Once you've written a Lisp function, one of the most important (and
most interesting) things you can do with it is to use it as a
building block to write more complicated functions.  But if your
function does funky things with its output, it will be much harder
to use it as a building block.

Here's a concrete example of what I mean:

Suppose you want to write a function that calculates the factorial
of a number.  You might be tempted to write something like this:

    ? (defun factorial (n)
        (check-type n (integer 1) "a positive integer")
        (loop for i from 2 to n
              and p = 1 then (* p i)
              finally (format t "The factorial of ~:d is ~:d." n p))
        (values))
    FACTORIAL
    ? (factorial 3)
    The factorial of 3 is 6.
    ? (factorial 30)
    The factorial of 30 is 265,252,859,812,191,058,636,308,480,000,000.

Well, don't do it!  The printed output looks nice, but there's no
good way to use this function as a building block.  Maybe the next
day you'll want to write a function that calculates the odds of
drawing a certain hand of cards from a deck of a certain size.
You'll end up wishing your factorial function just returned a
number, instead of printing a fancy output message.

If you really needed the fancy output message, it would be better
to write two functions: a plain factorial function that just returns
a number, and a separate output function that calls the main
factorial function and then does the fancy printing.

Here's a fun tip...

If you really like funky printing tricks, spend some time studying
FORMAT.  It won't necessarily make you a better Lisp programmer,
but you'll enjoy the thrills and chills!

And, by the way...

In article <··················@typhoon.nyroc.rr.com>,
Fred <········@NOSPAMrochester.rr.com> wrote:
> And when it is finished I would like to write over the Please wait:

There's no standard way to do this.  Even if you get it to work
with your Lisp system, it won't necessarily work with other systems.

-- Robert Munyer <······@mcs.com>