[mercury-users] returning a term from a function? how to?

Richard A. O'Keefe ok at atlas.otago.ac.nz
Thu May 3 14:30:19 AEST 2001


	1 - I used an in,in,out predicate, where I would have preferred a function:
	
	 a - how to notate (declare) such a function
	 b - how to write such a function
	 c - how to write code to get back such a term from a function
	
:- func add(rational, rational) = rational.

add(rational(N1,D1), rational(N2,D2)) = rational(N1*D1+N2*D2, D1*D2).

main -->
    { rational(A,B) = add(rational(1,3), rational(2,5)) },
    io__write_int(A), io__write_string("/"), io__write_int(B), nl.

Since Mercury is often compiled via some flavour of C, it is important
to ensure that text output ends with a newline.

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