[clean-list] newbie: simple console IO and 65536

Peter Achten peter88@cs.kun.nl
Tue, 11 Mar 2003 12:05:10 +0100


At 20:38 10-3-03 -0800, Isaac Gouy wrote:
>Puzzled by the behaviour of this simple console IO
>program (on Windows XP). After echoing back an
>inputline, 65536 appears on the console? How do I
>avoid this behaviour?
>
>---------------
>module echoline
>import StdEnv
>
>Start:: *World -> *World
>Start world
>         # (console, world) = stdio world
>           (ln, console) = freadline console
>           console = fwrites ln console
>           (ok, world) = fclose console world
>         |  not ok = abort "problem!"
>         = world
>---------------
>I:\Clean>echoline -con
>input line
>input line
>65536
>---------------

Other people have already commented on how to get rid of this magic number, 
allow me to explain why you get the number in the first place. Every Clean 
program attempts to print the result of the Start rule. In this case, the 
Start rule expects a value of type World and returns a new value of type 
World. The type World represents any external resource that your program 
might want to manipulate (such as the console in this program). The World 
happens to be implemented as an integer internally. In older Clean versions 
we used the bits of this integer to mark the use of particular 
subenvironments (external resources). A nicer representation would probably 
be something like "World" or even nothing at all as the world is already 
(t)here.

Hope this helps,
Peter Achten