users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: representing 'OR' condition in Prolog..


From: Fergus Henderson
Subject: Re: representing 'OR' condition in Prolog..
Date: Thu, 31 Oct 2002 18:43:06 +1100
User-agent: Mutt/1.3.28i

On 31-Oct-2002, Abhinav-Bhardwaj <address@hidden> wrote:
> I want to know something in general about prolog . I want to know that
> how do I represent an 'OR' condition in prolog.

Use either multiple clauses, or an explicit disjunction (the ";" infix
binary operator).

> For eg:- I want to define a leave rule saying that an employee can be granted 
> a leave if - employee is a Permanent employee, OR
>           -  employee's performance is satisfactory.
> 
> leave_granted(Emp) :- employee_status(Emp, permanent).
> leave_granted(Emp) :- performance(Emp, satisfactory).

That would be fine.

Another way to write it is

        leave_granted(Emp) :-
                ( employee_status(Emp, permanent)
                ; performance(Emp, satisfactory)
                ).

but using separate clauses like you did is preferable in this case, IMHO.

...
> leave_report :-
>         nl, write('leave may be granted to following employees:-'), nl,
>         leave_granted(Emp),
>         write('-'), write(Emp), nl,
>         fail.
...
> 2) The 'leave_report' displays the name 'raman' twice , and what I
> want is a list of unique names of employees that can be granted leave.

Use setof/3.

In general, I consider it bad style to write explicit failure-driven loops.
It's nicer style to use an all-solutions predicate such as
findall/3, bagof/3 or setof/3, and then iterate over the list.

-- 
Fergus Henderson <address@hidden>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]