[mercury-users] Some more Queries

Ralph Becket rafe at cs.mu.OZ.AU
Sun Nov 10 10:46:41 AEDT 2002


Noel  Pinto, Saturday,  9 November 2002:
> 		io__read_binary(Str, ResStr `with_type` io__result(string)),
> 		(
> 			{ResStr = ok(Binr `with_type` string)}
> 		->
> 			io__write_string(" Binary representation of a term 
> 			is : "),
> 			io__write_binary(Binr `with_type` string), nl
> 		;
> 			{ ResStr = eof }
> 		->
> 			io__write_string(" End of File.\n")
> 		;
> 			io__write_string(" Error occured.\n"),
> 			{ ResStr = error(Err1)} % Some error occurs, thats 
> 			why I am writing this part
> 			->
> 			{ io__error_message(Err1, ErrMesg)},
> 			io__write_string(ErrMesg)
> 			;
> 				[] % upto this part I have added from the 
> 				last time to check why it comes here directly
> 		)

First of all, since you're testing every possible binding of ResStr, you
should use a switch rather than an if-then-else chain.  That is, your
code should look like this:

> 		io__read_binary(Str, ResStr `with_type` io__result(string)),
> 		(
> 			{ResStr = ok(Binr `with_type` string)},
> 			io__write_string(" Binary representation of a term is : "),
> 			io__write_binary(Binr `with_type` string), nl
> 		;
> 			{ ResStr = eof },
> 			io__write_string(" End of File.\n")
> 		;
> 			io__write_string(" Error occured.\n"),
> 			{ ResStr = error(Err1)} % Some error occurs, thats 
> 			why I am writing this part
> 			->
> 			{ io__error_message(Err1, ErrMesg)},
> 			io__write_string(ErrMesg)
> 			;
> 				[] % upto this part I have added from the 
> 				last time to check why it comes here directly
> 		)

Now your problem is that in the third arm of the switch, you (a) haven't
put the test first, which unfortunately does matter in this case, and
(b) you haven't put parentheses around the if-then-else there, so the
compiler thinks that the call to io__write_string is part of the
if-then-else condition, which is wrong because the condition can fail
and io__write_string has unique arguments, and (c) you don't need an
if-then-else there at all.  Here's how the last part should look:

> 			{ ResStr = error(Err1)},
> 			io__write_string(" Error occured.\n"),
> 			{ io__error_message(Err1, ErrMesg)},
> 			io__write_string(ErrMesg)

- Ralph
--------------------------------------------------------------------------
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