[mercury-users] Style, Newbie decision question, "What? No X?!", Microbenchmark

Julian Fondren cleverjulian at hotmail.com
Thu Nov 21 02:32:16 AEDT 2002


>It is a matter of style, but only die-hard Prolog programmers would
>argue that predicate style is preferable to functional style given the
>choice :-)

OK =)

>char_filter(X, Y) :-
>	(	X = '\t', Y = '\n'
>	;	X = C,    Y = C
>	).
>
>where the non-determinism should be obvious.

Yes, thank you.

>Please use ( if _ then _ else _ ) rather than ( _ -> _ ; _ ).

Even the Mercury documentation seems consistent in its use of
( _ -> _ ; _ ); why do you prefer ( if _ then _ else _ )?  (Another
style issue I forgot to question: should ':' or '__' be used for
fully-qualified imports?  Erlang uses ':', so I've become fond of
that, but __ is also fairly consistently used.)

>You only need parentheses around the top-level if-then-else.  You can
>chain conditional goals like this:
>
>is_whitespace(C, Bool) :-
>	(      if C = ' '  then Bool = yes
>	  else if C = '\t' then Bool = yes
>	  else if C = '\r' then Bool = yes
>	  else if C = '\n' then Bool = yes
>	  else                  Bool = no
>	).

>[Again, this should probably be a func if you insist on having a bool
>result.]

OK.

>is_whitespace(C, Bool) :-
>	Bool = ( if   ( C = ' ' ; C = '\t' ; C = '\r' ; C = '\n' )
>	         then yes
>	         else no
>	       ).

Wow, OK =)

>chomp(S0) = ( if string__remove_suffix(S0, "\n", S) then S else S0 ).

(almost following your earlier style:)

chomp(S0) =
  (      if string__remove_suffix(S0, "\r\n", S) then S
    else if string__remove_suffix(S0, "\n",   S) then S
    else                                              S0
  ).

... but it occurs to me that I don't really need the first case.
Thanks for remove_suffix/3, I must've overlooked it.

>main(!IO) :-
>     io__read_char(Result, !IO),
>     (
>         Result = eof
>     ;
>         Result = error(_),
>         throw(Result)
>     ;
>         Result = ok(Char),
>         ( if Char = '\n' then io__write_string("\r\n", !IO)
>                          else io__write_char(Char,     !IO) ),
>         main(!IO)
>     ).

Thanks again: !IO is new syntax on me.



_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*. 
http://join.msn.com/?page=features/virus

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