[net.lang.pascal] Interactive I/O in Pascal

root@topaz.RUTGERS.EDU (Charles Hedrick) (07/30/86)

You are right that I have heard the complaint about interactive I/O a
number of times in the past.  However it has appeared much less often
recently, since the various implementations seem to be converging on
the use of "lazy I/O".  Most implementors seem to believe that the ISO
standard implies that this is the correct way to handle interactive
I/O.  You complain that the following program will print an extra
prompt.

	writeln ('enter an integer : ');
	while not eof do
	   begin
	      readln (i);
	      writeln ('enter an integer : ');
	   end;

As I see it, you will have roughly the following dialog:

  enter an integer: 123
  enter an integer: ^D

at which point the program is finished.  I don't see what is wrong
with that.  Perhaps you want

  enter an integer: 123^D

If you want to be able to recognize that, you will simply have to
use a separate call to readln.  Presuming that you want to be able
to recognize ^D in either context, the program would look like

	while true do 
	   begin
	   writeln ('enter an integer : ');
	   if eof 
	     then {exitloop}				goto 666;
	   read (i);
	   if eof
	     then {exitloop}				goto 666;
	   readln;
	   end;						666:

This tests for eof twice because I assume you want to allow it either
place. (Sorry about the goto's, but the logic is clearer that way than
with any alternative I could think of.)

As far as I know, the semantics of lazy I/O allow you to check for any
possible input, printing prompts at any point.  This does not mean
that Pascal's input parsing is as powerful as scanf, or that its
output is as powerful as printf.  Obviously they are not.  Validating
input, to make sure that the user hasn't typed something bogus, can
require a bit more programming in Pascal than in C.  For example, in
the above programs, if the user types an extra CR before the ^D the
program may think that there is one more number than there is.  If you
want the program to be very careful about checking validity of its
input, you will need to write code to check that the user has typed
one and exactly one number on each line.  But as far as I know, you
can always find a way to avoid having extra prompts and misplaced
input hangs.

crs@lanl.ARPA (Charlie Sorsby) (08/01/86)

> As far as I know, the semantics of lazy I/O allow you to check for any
> possible input, printing prompts at any point.  This does not mean
> that Pascal's input parsing is as powerful as scanf, or that its
> output is as powerful as printf.  Obviously they are not.  Validating
> input, to make sure that the user hasn't typed something bogus, can
> require a bit more programming in Pascal than in C.  For example, in

Am I missing something?

As I understand it, printf and scanf are not considered part of the C
language but rather are part of the standard I/O package.  Is this also
true for writeln and readln in Pascal?  If so, it appears that standard
I/O packages are being compared rather than languages.  If not, perhaps the
comparisons are between apples and oranges.

Is there some characteristic of the Pascal language that prevents
construction of a Pascal standard I/O package that *would* be as powerful
as that of C?  Perhaps the existence of this characteristic is implicit in
this discussion and that is what I am missing.  If so, forgive my
ignorance.  (I realize that the lack of "separate compilation" in Pascal
precludes use of precompiled libraries of functions.)

Of course, there is also the fact that Pascal was intended as a teaching
language to consider.
-- 
Charlie Sorsby
	...!{cmcl2,ihnp4,...}!lanl!crs
				crs@lanl.arpa

root@topaz.RUTGERS.EDU (Charles Hedrick) (08/02/86)

Because Pascal does strong type-checking, it would be hard to write
anything like printf and scanf for it.  Indeed if writeln and readln
were not built into the compiler, you would not be able to define them
yourself.  A Pascal procedure must have a fixed number of arguments,
each of a definite type.  Thus the best you could do would be
  printint(int i);
  printreal(real r);
etc.  Unfortunately, I/O libraries are not something that can be
tacked on later.  I/O affects the definition of the language.  This is
one of the most serious problems with the languges in the Algol
tradition.  There is a tendency to think I/O is a detail unfit for
computer scientists, which can be left to the "standard prolog".

gvcormack@watmum.UUCP (Gordon V. Cormack) (08/02/86)

> 
> Am I missing something?
> 
> Is there some characteristic of the Pascal language that prevents
> construction of a Pascal standard I/O package that *would* be as powerful
> as that of C?  Perhaps the existence of this characteristic is implicit in
> this discussion and that is what I am missing.  

There are lots of reasons one can't write printf in Pascal, and they
all contribute to the conclusion that Pascal is garbage.  Just
for starters, you can't (in Wirth's Pascal, anyway) even write
a procedure that takes a quoted string as a parameter, unless its
length is hard coded.  Next, you can't write a procedure that takes
a variable number of parameters.  Finally, you can't write a
*polymorphic* procedure that operates on many types, nor can you
even overload the name "printf" to make it apply to many types.

I wouldn't want anyone to take this article as support for C.
For a long time I have been suppressing the urge to post an article
to the effect:

   STOP!!  You're both wrong.
-- 
Gordon V. Cormack      CS Department, University of Waterloo
uucp:  { allegra, decvax, ... }!watmath!gvcormack
csnet: gvcormack%watmum@waterloo
cdn:   gvcormack@mum.waterloo.cdn

ali@navajo.STANFORD.EDU (Ali Ozer) (08/04/86)

In article <563@watmum.UUCP> gvcormack@watmum.UUCP (Gordon V. Cormack) writes:
>...Just for starters, you can't (in Wirth's Pascal, anyway) even write
>a procedure that takes a quoted string as a parameter, unless its
>length is hard coded...

True, but thanks to conformant arrays, you can do stuff like:

procedure Foo (str: packed array [lo..hi: integer] of char);

Then calls to Foo with packed arrays of any length are legal. And conformant
arrays are part of the Pascal standard  now, aren't they?

Ali, ali@su-score.arpa, ali@navajo

jsdy@hadron.UUCP (Joseph S. D. Yao) (08/10/86)

In article <5918@lanl.ARPA> crs@lanl.ARPA (Charlie Sorsby) writes:
	[attribution omitted]
>> As far as I know, the semantics of lazy I/O ...
>As I understand it, printf and scanf are not considered part of the C
>language but rather are part of the standard I/O package.  Is this also
>true for writeln and readln in Pascal?  If so, it appears that standard
>I/O packages are being compared rather than languages.  ...

Yes, you are missing something.  Pascal I/O is part of the language,
and not part of any library.  It is a "built-in."
-- 

	Joe Yao		hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}
			jsdy@hadron.COM (not yet domainised)