For Programmers: Free Programming Magazines  


Home > Archive > Prolog > February 2005 > If-Present









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author If-Present
sudha

2005-02-14, 8:59 am

Hi please some one help me with this:

case:

if-present(X, X) else if-present (A,B,C,D,E "string") :-

If there is variable X present write X, else if there are A,B,C,D,E
variables present then write the string , "string".
How to write a function for this in prolog?

regards,
Sudha

George SP.

2005-02-14, 8:58 pm

Hi,
I am not quite sure if i understand your question completely. Are you trying
to write a Prolog predicate that would behave like the if-then structure in
other languages?

Here is my guess:
--------------------------------
Other languages:
if (condition1 and cond2 and cond3....etc.) then action

In Prolog:
rule :- condition1, cond2,cond3,....etc.,!, action.

cond1,2,3,...etc. could be any logical statement or predicate
---------------------------------
Other languages:
if (cond1 or cond2 or cond3 or .....etc) then action

In Prolog
rule:-cond1,!,action.
rule:-cond2,!,action.
rule:-cond3,!,action.
..................
rule:-condN!,action.
-----------------------------------
Other languages
If 'something' then 'something1' elseif 'something2' then 'something3' else
'something4' :-)

In prolog
rule:- something,!,something1.
rule:-something2,!,something3.
rule:-something4.
------------------------------------

ANY good???

"sudha" <navred@rediffmail.com> wrote in message
news:cc6fac18bfd94b3aac35fa70e559625a@lo
calhost.talkaboutprogramming.com...
> Hi please some one help me with this:
>
> case:
>
> if-present(X, X) else if-present (A,B,C,D,E "string") :-
>
> If there is variable X present write X, else if there are A,B,C,D,E
> variables present then write the string , "string".
> How to write a function for this in prolog?
>
> regards,
> Sudha
>



W

2005-02-15, 4:01 am

Variables in prolog only "exist" in the one clause in which they are
mentioned.
So I don't know how to interpret what you mean by "variable X present".
also, try not to think of "do this and that and then write string".
rather, think of some relation you are trying to express.

But I will put on my C-Java-etc hat and try.
by "present" you may be thinking that there is some variable that has a
value.
so let us think of that as a fact in prolog, such as
x(abc).

so the prolog goal
:-x(X).
would succeed, binding X in that case to abc.

if that fact were not provable, than that goal would fail.

so the truth you may be getting at is:
Sudha_goal(String) is true when x(String) is true.
Sudha_goal('sudha string') is true when a is true and b is true and...and e
is true.

this would be in prolog:

sudha_goal(String) :- present_X(String).
sudha_goal(strint) :- a,b,c,d,e.

SOmething like this.

W
"sudha" <navred@rediffmail.com> wrote in message
news:cc6fac18bfd94b3aac35fa70e559625a@lo
calhost.talkaboutprogramming.com...
> Hi please some one help me with this:
>
> case:
>
> if-present(X, X) else if-present (A,B,C,D,E "string") :-
>
> If there is variable X present write X, else if there are A,B,C,D,E
> variables present then write the string , "string".
> How to write a function for this in prolog?
>
> regards,
> Sudha
>



sudha

2005-02-16, 9:00 pm

thanks for you reply.



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com