[mercury-users] Re: Mercury Tutorial / Functions

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Apr 25 03:16:24 AEST 2003


On 24-Apr-2003, Goncalo Jorge Coelho e Silva <l10454 at alunos.uevora.pt> wrote:
> 
> %main --> main(IO0,IO):-
>         read_strings(Ss,IO0,IO). 

The first line there is ALL commented out.
Presumably you intended to write this?

	main(IO0,IO):-
		read_strings(Ss,IO0,IO). 

> read_strings(Ss, IO0, IO) :-
>         read_strings_2([], Ss, IO0, IO). 
> 
> read_strings_2(RevSs, Ss, IO0, IO) :-
>         io__read_line_as_string(Result, IO0, IO1),
>         (
>                 Result = ok(S),
>                 %io__write_string(S,IO0, IO1),
>                 IO = IO1,
>                 Ss = [S | RevSs],
>                 read_strings_2([S | RevSs], Ss, IO1, IO) 
>         ; 
>                 Result = eof,
>                 Ss = list__reverse(RevSs) 
>         ; 
>                 Result = error(_),
>                 exception__throw(Result) 
>         ). 

The line "IO = IO1" needs to go in the second disjunct, not the
first disjunct.  The first disjunct already binds "IO" in
the recursive call to read_strings_2; the mode error in the
original program was because the second disjunct never bound "IO".

So it should look like this:

	read_strings_2(RevSs, Ss, IO0, IO) :-
		io__read_line_as_string(Result, IO0, IO1),
		(
			Result = ok(S),
			Ss = [S | RevSs],
			read_strings_2([S | RevSs], Ss, IO1, IO) 
		; 
			Result = eof,
			Ss = list__reverse(RevSs),
			IO = IO1
		; 
			Result = error(_),
			exception__throw(Result) 
		). 

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  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