Subject: Best-practice for if/then/else?

From: Robb Shecter <[email-masked]>
Date: Mon, 15 May 2000 15:59:06 +0200

Hi,

I'm using SWI to add intelligence to an XML-based object trading
system.  (Shout if you've done something similar...)

At any rate, I've learned Prolog from a few books that seem to be a
bit old, and don't cover practical, day-to-day needs like if/thens.
I've posted questions on usenet, and looked around the web, but have
gotten conflicting information about the -> operator, whether it's a
"good practice" to use it, the "otherwise" keyword, etc.  I've seen
people who say always use it, and others who say to avoid it.

I have to admit that the precedence issues of , and ; confuse me still
as well as the different types of cut.

So, can somebody give a simple template for the accepted way of doing
an if/then, if/then/else, or then possibly a case statement?  I'm also
a bit confused by parentheses.  I had assumed that they can be used as
in other languages - to enforce order of operator evaluation, but I'm
not sure if that's the case with (SWI) Prolog.

Here's a concrete example of the kind of code I've been writing:
(Without if/then:)

%%
%% Calculate the total latency for a given server list.  This accounts

%% for some servers not haveing "average_time" facts.
%%

sll([], 0).

sll([Server|Tail], L) :-
    average_time(Server, T),
    sll(Tail, L2),
    L is T + L2.

server_list_latency(ServerList, Latency) :-
 (
     sll(ServerList, Latency), !
 ) ;
 (
     Latency = 'unknown'
 ).

And here's an example of where I try to use if/then:

%%
%% Find the minimum reliability of all servers in a list.
%% Usage: minimum_reliability(List, Reliability).
%%
mr([Server|Tail], min(R, Min)) :-
    reliability(Server, R),
    mr(Tail, Min).

minimum_reliability(List, Reliability) :-
 ( mr(List, R) ->
     Reliability is R
 ; otherwise ->
     Reliability = 'unknown'
 ).

Thanks for any hints,
Robb