[mercury-users] reading term

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Feb 14 06:02:30 AEDT 2005


On 07-Feb-2005, Elmar Haneke <elmar at haneke.de> wrote:
> 
> io__read seems to be very much more complicated than "read" predicate 
> in prolog.

It's a little bit more complicated, but not much.
Prolog makes it very easy to do the wrong thing with EOF.
Mercury's io.read is designed to make the programmer think
about what happens in the end-of-file case.

> 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.
> 
> Is there really no such simple thing in Mercury?

In Mercury, you can write

	read(R),
	( {R=ok(T)} -> process(T) ; {throw(R)} )

and this will have a similar effect to the Prolog code.
If you really want, you can write your own version of read:

	:- pred my_read(T::out, io::di, io::uo) is det.
	my_read(T) -->
		read(R),
		{ R=ok(T1) -> T = T1 ; throw(R) }.

and then you can use

	my_read(T),
	process(T).

-- 
Fergus Henderson                    |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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