[mercury-users] Perl shift || $defalt

Tyson Dowd trd at cs.mu.OZ.AU
Fri Apr 27 00:03:06 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)]).
> 
> It's not quite the same since `shift' returns the first argument and
> then removes it from argv; in Mercury you have to handle the list
> explicitly.

A more general solution would be to provide a list of defaults and 
do something like:

default_args(["12", "42", "1066"]).

get_command_line_args(Args) -->
	io__command_line_args(ArgV),
	{ default_args(Defaults) },
	{ RemainingDefaults = list__drop(list__length(ArgV), Defaults) ->
		Args = list__append(ArgV, RemainingDefaults)
	;
		Args = ArgV 
	}.

Completely untested of course.

-- 
       Tyson Dowd           # 
                            #  Surreal humour isn't everyone's cup of fur.
     trd at cs.mu.oz.au        # 
http://www.cs.mu.oz.au/~trd #
--------------------------------------------------------------------------
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