From: ············@gmail.com
Subject: loop basics
Date: 
Message-ID: <1170841525.220296.107600@s48g2000cws.googlegroups.com>
hi,

I'm learning common lisp using sbcl. To study both the terminal IOs
and the loop macro, I started with what I thought would have been
easy: a simple echo function within an infinite loop. It looks like
this:
(defun echo ()
  (loop (format t (read-line t))))

But then definning this at the REPL and then calling (echo), i never
get the formatted output. What am I misunderstanding ?

-Nicolas

From: Timofei Shatrov
Subject: Re: loop basics
Date: 
Message-ID: <45c9a173.17352391@news.readfreenews.net>
On 7 Feb 2007 01:45:25 -0800, ············@gmail.com tried to confuse everyone
with this message:

>hi,
>
>I'm learning common lisp using sbcl. To study both the terminal IOs
>and the loop macro, I started with what I thought would have been
>easy: a simple echo function within an infinite loop. It looks like
>this:
>(defun echo ()
>  (loop (format t (read-line t))))
>
>But then definning this at the REPL and then calling (echo), i never
>get the formatted output. What am I misunderstanding ?
>

Oh my god! Did you never heard of the control-string exploit? Control strings
shouldn't be ever entered from keyboard. This is however a style issue and
doesn't explain the result you experienced. You may notice that your code never
prints #\Newline character, so everything is printed in one long line. So your
implementation (or the underlying OS) may be waiting for that line to end to
print the text. You can either use (finish-output) to override that behavior, or
just print #\Newline after each line. For example:

(defun echo ()
  (loop (format t "~a~%" (read-line))))

-- 
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|
From: ············@gmail.com
Subject: Re: loop basics
Date: 
Message-ID: <1170844574.915192.321610@v33g2000cwv.googlegroups.com>
On Feb 7, 11:13 am, ····@mail.ru (Timofei Shatrov) wrote:
> On 7 Feb 2007 01:45:25 -0800, ············@gmail.com tried to confuse everyone
> with this message:
>
> >hi,
>
> >I'm learning common lisp using sbcl. To study both the terminal IOs
> >and the loop macro, I started with what I thought would have been
> >easy: a simple echo function within an infinite loop. It looks like
> >this:
> >(defun echo ()
> >  (loop (format t (read-line t))))
>
> >But then definning this at the REPL and then calling (echo), i never
> >get the formatted output. What am I misunderstanding ?
>
> Oh my god! Did you never heard of the control-string exploit? Control strings
> shouldn't be ever entered from keyboard. This is however a style issue and
> doesn't explain the result you experienced.

Well, at time being i'm just learning lisp fundamentals with the REPL,
not making any secure-critical program ;)

> You may notice that your code never
> prints #\Newline character, so everything is printed in one long line. So your
> implementation (or the underlying OS) may be waiting for that line to end to
> print the text. You can either use (finish-output) to override that behavior, or
> just print #\Newline after each line. For example:
>
> (defun echo ()
>   (loop (format t "~a~%" (read-line))))
>

Ok, that works. I didn't think the issue was there since i also tried
the following:
(defun echo ()
  (loop (format t "~a"  (read-line))
        (force-output t)))

but that either failed (as with finish-output instead of force-
output). I thought flushing the output would have ... flushed it ;) So
what if I want to print strings without any newline ? Note I'm using
SBCL 1.0 on RHEL3 Linux box.

-Nicolas
From: Pascal Bourguignon
Subject: Re: loop basics
Date: 
Message-ID: <871wl22o2t.fsf@thalassa.informatimago.com>
············@gmail.com writes:
> Ok, that works. I didn't think the issue was there since i also tried
> the following:
> (defun echo ()
>   (loop (format t "~a"  (read-line))
>         (force-output t)))
>
> but that either failed (as with finish-output instead of force-
> output). I thought flushing the output would have ... flushed it ;) So
> what if I want to print strings without any newline ? Note I'm using
> SBCL 1.0 on RHEL3 Linux box.

This is because you are not flushing the stream you are writting to!

With FORMAT, the _destination_ T means *STANDARD-OUTPUT*.
With FORCE-OUTPUT, the _stream designator_ T means *TERMINAL-IO*.

Try:

 (defun echo ()
   (loop (format *standard-output* "~a"  (read-line))
         (force-output *standard-output*)))

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

This is a signature virus.  Add me to your signature and help me to live.
From: ············@gmail.com
Subject: Re: loop basics
Date: 
Message-ID: <1170852275.184101.275450@v33g2000cwv.googlegroups.com>
On Feb 7, 12:40 pm, Pascal Bourguignon <····@informatimago.com> wrote:
> ············@gmail.com writes:
> > Ok, that works. I didn't think the issue was there since i also tried
> > the following:
> > (defun echo ()
> >   (loop (format t "~a"  (read-line))
> >         (force-output t)))
>
> > but that either failed (as with finish-output instead of force-
> > output). I thought flushing the output would have ... flushed it ;) So
> > what if I want to print strings without any newline ? Note I'm using
> > SBCL 1.0 on RHEL3 Linux box.
>
> This is because you are not flushing the stream you are writting to!
>
> With FORMAT, the _destination_ T means *STANDARD-OUTPUT*.
> With FORCE-OUTPUT, the _stream designator_ T means *TERMINAL-IO*.
>
> Try:
>
>  (defun echo ()
>    (loop (format *standard-output* "~a"  (read-line))
>          (force-output *standard-output*)))
>

I should indeed read the functions documentation more carefully.
Thanks.
From: Frank Buss
Subject: Re: loop basics
Date: 
Message-ID: <idoxcbnq629m.ly7wo78kkmv7$.dlg@40tude.net>
············@gmail.com wrote:

> (defun echo ()
>   (loop (format t (read-line t))))
> 
> But then definning this at the REPL and then calling (echo), i never
> get the formatted output. What am I misunderstanding ?

In LispWorks 4.3.7 it works (but it shows a DOS box for entering the text).
And with read-line you have to hit "enter" for showing the line.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de