For Programmers: Free Programming Magazines  


Home > Archive > Prolog > February 2005 > Writing functions for If-else statement in PROLOG









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 Writing functions for If-else statement in PROLOG
sudha

2005-02-13, 8:59 am

Hi, I want to write a function in prolog for the if-else statement.
For ex,
if-instantiated( X,X) else if-instantiated (Y,Y) else if-instantiated(
X^Y, concat(X,Y):-
If the attribute X is instantiated( holds some
value) then write X, else if the attribute Y holds some value then write
Y, else if both the attributes X and Y hold some values, then append X
and
Y and write them.

I know how to write the concat function. But I am unaware of writing the
function for the above if-else rule. please help me.
Thanks in advance,
SUDHA.P


Jan Burse

2005-02-13, 8:59 am

show(X,Y) :- \+var(X), var(Y),
write('x:'), write(X).
show(X,Y) :- var(X), \+var(Y),
write('y:'), write(Y).
show(X,Y) :- \+var(X), \+var(Y),
write('x:'), write(X), write('y:'), write(Y).

sudha wrote:
> Hi, I want to write a function in prolog for the if-else statement.
> For ex,
> if-instantiated( X,X) else if-instantiated (Y,Y) else if-instantiated(
> X^Y, concat(X,Y):-
> If the attribute X is instantiated( holds some
> value) then write X, else if the attribute Y holds some value then write
> Y, else if both the attributes X and Y hold some values, then append X
> and
> Y and write them.
>
> I know how to write the concat function. But I am unaware of writing the
> function for the above if-else rule. please help me.
> Thanks in advance,
> SUDHA.P
>
>

W

2005-02-15, 4:01 am

X and Y would not be "attributes". From your question, i think you may be
having problems with the concept of logical variable.
Variables' scope is the one clause in which they appear.

Although there are occasions when a prolog programmer will want to know if a
variable is instantiated, that should be considered "unusual".

also, if A then B else C in prolog is expressed A -> B;C
but this is just syntactic sugar for
....:-A,B...
....:-C,...

Of course, here A, B, C must be prolog terms.

Walter

"sudha" <navred@rediffmail.com> wrote in message
news:efe3d3e9f55e233979c988cebaae5196@lo
calhost.talkaboutprogramming.com...
> Hi, I want to write a function in prolog for the if-else statement.
> For ex,
> if-instantiated( X,X) else if-instantiated (Y,Y) else if-instantiated(
> X^Y, concat(X,Y):-
> If the attribute X is instantiated( holds some
> value) then write X, else if the attribute Y holds some value then write
> Y, else if both the attributes X and Y hold some values, then append X
> and
> Y and write them.
>
> I know how to write the concat function. But I am unaware of writing the
> function for the above if-else rule. please help me.
> Thanks in advance,
> SUDHA.P
>
>



Sponsored Links







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

Copyright 2008 codecomments.com