[mercury-users] reading term

Julien Fischer juliensf at cs.mu.OZ.AU
Mon Feb 7 21:20:16 AEDT 2005


On Mon, 7 Feb 2005, Elmar Haneke wrote:

> Philippe Teuwen schrieb:
>
> > Use 'io__read'.
> > "Reads a ground term of any type, written using standard
> > Mercury syntax, from the current or specified input stream.
> > The type of the term read is determined by the context
> > in which 'io__read' is used."
>
> io__read seems to be very much more complicated than "read" predicate
> in prolog. What I get on
>
> 	reat(T),print(T)
>
> is an rather long term() structure nt the term I thyped in before.
>
>
That won't quite work in Mercury because the value returned from io.read
has type io.read_result.

> In Prolog I just can replace
>
> 	T=[(1,2,3),(3,4,5)],
> 	process(T)
>
> by
>
> 	read(T),
> 	process(T)
>
> if the string "[(1,2,3),(3,4,5)]" comes from stdin.
>

The following Mercury program reads in a list of int 3-tuples from
stdin and print the result using io.print.

	:- module read_write.

	:- interface.

	:- import_module io.

	:- pred main(io::di, io::uo) is det.

	:- implementation.

	:- import_module int, list.

	main(!IO) :-
		io.read(ReadResult, !IO),
		(
			ReadResult = ok(X `with_type` list({int,int,int})),
			io.print(X, !IO)
		;
			ReadResult = eof
		;
			ReadResult = error(_,_)
		).

	:- end_module read_write.

Given the the following input:

	[{1,2,3},{3,4,5].

it will print:

	[{1, 2, 3}, {3, 4, 5}]

Cheers,
Julien.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list