From shecter@darmstadt.gmd.de  Mon May 15 15:55:53 2000
Received: from sonne.darmstadt.gmd.de (sonne.darmstadt.gmd.de [141.12.62.20])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id PAA17443
	for <prolog@swi.psy.uva.nl>; Mon, 15 May 2000 15:55:53 +0200 (MET DST)
Received: from darmstadt.gmd.de (robb@pc-ginsberg [141.12.33.149])
	by sonne.darmstadt.gmd.de (8.8.8/8.8.5) with ESMTP id PAA03054
	for <prolog@swi.psy.uva.nl>; Mon, 15 May 2000 15:55:38 +0200 (MET DST)
Sender: robb@darmstadt.gmd.de
Message-ID: <392002AA.D8F01DFB@darmstadt.gmd.de>
Date: Mon, 15 May 2000 15:59:06 +0200
From: Robb Shecter <shecter@darmstadt.gmd.de>
X-Mailer: Mozilla 4.7 [en] (X11; I; Linux 2.2.13 i686)
X-Accept-Language: en, de, fr
MIME-Version: 1.0
To: "prolog@swi.psy.uva.nl" <prolog@swi.psy.uva.nl>
Subject: Best-practice for if/then/else?
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

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

