[mercury-users] string__det_to_int

Ralph Becket rafe at cs.mu.OZ.AU
Sat Nov 16 11:54:33 AEDT 2002


Noel  Pinto, Friday, 15 November 2002:
> 
> main -->
> 	print(" Enter an integer : "), flush_output,
> 	io__read_line_as_string(IntString),
> 	{
> 		IntString = ok(Int_line),
> 		remove_suffix(Int_line, "\n", ResStr),
> 		string__det_to_int(ResStr) = ResDetInt,
> 		ResDt = ResDetInt,
> 		;
> 		throw(" Error")
> 	}, nl,
> 	print(" A signed base of 10 converted to an integer is : "),
> 	print(ResDt), nl,

I don't believe this program will compile unless you have declared
main/2 to be cc_multi: main/2 has to be det or cc_multi, but this
definition contains a disjunction, which makes it multi.

A switch is essentially a special case of a disjunction which the
compiler recognises as being det, because each disjunct depends upon a
particular variable being bound to a different constructor.

What you need here is something like

:- import_module string, int, list, exception.

main -->
	io__format("Enter an integer: ", []),
	io__flush_output,
	io__read_line_as_string(Result),
	{
		Result = error(_),
		throw(Result)
	;
		Result = eof,
		Out    = "EOF"
	;
		Result = ok(Line0),
		( if
			string__remove_suffix(Line0, "\n", Line),
			string__to_int(Line, Int)
		  then
		  	Out = string__format("%d", [i(Line)])
		  else
		  	Out = "that doesn't look like an int"
		)
	},
	io__format("%s\n", [s(Out)]).

> What I want to understand is if I give a value to IntString as 
> 123, then ResDet also gives me the value as 123. I really cannot 
> see how  string__det_to_int works. I really would like to know.

	string__to_int("123", X)	succeeds with X = 123
	string__to_int("-123", X)	succeeds with X = -123
	string__to_int("+123", X)	succeeds with X = 123
	string__to_int(" 123", X)	fails
	string__to_int("abc", X)	fails

	X = string__det_to_int("123")	succeeds with X = 123
	X = string__det_to_int("-123")	succeeds with X = -123
	X = string__det_to_int("+123")	succeeds with X = 123
	X = string__det_to_int(" 123")	throws an exception
	X = string__det_to_int("abc")	throws an exception

To use string__det_to_int in our example program we'd write:

:- import_module string, int, list, exception.

main -->
	io__format("Enter an integer: ", []),
	io__flush_output,
	io__read_line_as_string(Result),
	{
		Result = error(_),
		throw(Result)
	;
		Result = eof,
		Out    = "EOF"
	;
		Result = ok(Line0),
		Line   = ( if   string__remove_suffix(Line0, "\n", Line1)
			   then Line1
			   else Line0
			 ),
		Out    = string__format("%d", [i(string__det_to_int(Line))])
	},
	io__format("%s\n", [s(Out)]).

which is arguably simpler, but doesn't attempt to report malformed input
other than by throwing an exception.

- 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