[mercury-users] Perl shift || $defalt

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Apr 27 02:13:51 AEST 2001


On 26-Apr-2001, Ralph Becket <rbeck at microsoft.com> wrote:
> > In Perl, it is very easy to say the following:
> > 
> >   the value of x is equal to the first data item in ARGV,
> >   or if ARGV is not defined then use the value 12:
> > 
> > $x = shift || 12;
> > print "$x\n";
> > 
> > Thus, a program can either take values on STDIN or use hardcoded
> > defaults with relative ease.
> > 
> > Could someone show how to do this same thing in Mercury?
> 
> main -->
> 	io__command_line_args(ArgV),
> 	{ if ArgV = [Arg1 | _] then X = Arg1 else X = "12" },
> 	io__format("%s\n", [s(X)]).

You can also write it a little closer in style to the original example:

	main -->
		io__command_line_args(ArgV),
		{ X = (if ArgV = [Arg1 | _] then Arg1 else "12") },
	 	io__format("%s\n", [s(X)]).

In fact, if you prefer Perl-style obfuscation, you can get MUCH more concise:

	main --> command_line_args(ArgV), print(ArgV=[Arg1|_]->Arg1;"12"), nl.

Real Programmers can write Fortran^H^H^H^H^H^HPerl in any language ;-)

Cheers,
	Fergus 

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "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