For Programmers: Free Programming Magazines  


Home > Archive > Prolog > March 2004 > Logical Or









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 Logical Or
Linux Man

2004-03-27, 12:11 am

hi

How can I use "or"? I want to do some calculations when in procedure
"myPrint(A)", A is either 5 or 10.

Thanks in advance
LinuxMan

P.S. I'm using SWI-Prolog
Benjamin Johnston

2004-03-27, 12:11 am


myPrint(A) :-
(
A = 5,
write(five)
;
A = 10,
write(ten)
).

(the semicolon means "or").

but you can often do "or" just by having multiple clauses:

myPrint(A) :- A = 5, write(five).
myPrint(A) :- A = 10, write(ten).

Is that what you are asking?

-Benjamin Johnston

"Linux Man" <steven_82m@hotmail.com> wrote in message
news:150a16c6.0403241334.203e8b2d@posting.google.com...
> hi
>
> How can I use "or"? I want to do some calculations when in procedure
> "myPrint(A)", A is either 5 or 10.
>
> Thanks in advance
> LinuxMan
>
> P.S. I'm using SWI-Prolog



Linux Man

2004-03-27, 12:11 am

Hi Again,

About the OR, maybe my question wasn't clear.
The program I want is: (the syntax is in Java, but I need the Prolog syntax)
if ( (A==5) || (A==10) )
{
print('Hello from the World to Benjamin');
}

I don't want to use the "print" command twice. That's I do NOT want:
myPrint(A) :- A=5, print('Hello from the World to Benjamin').
myPrint(A) :- A=10, print('Hello from the World to Benjamin').

Thanks in advance
Steve
Stefan Ljungstrand

2004-03-27, 12:11 am

On Thu, 24 Mar 2004, Linux Man wrote:

> Hi Again,
>
> About the OR, maybe my question wasn't clear.
> The program I want is: (the syntax is in Java, but I need the Prolog syntax)
> if ( (A==5) || (A==10) )
> {
> print('Hello from the World to Benjamin');
> }
>
> I don't want to use the "print" command twice. That's I do NOT want:
> myPrint(A) :- A=5, print('Hello from the World to Benjamin').
> myPrint(A) :- A=10, print('Hello from the World to Benjamin').


I can understand that.

So, you could of course abstract out the differing parts into a separate
predicate (or sometimes more than one), like :

myPrint(A) :-
fiveOrTen(A),
print('Hello from the World to Benjamin').


fiveOrTen(A) :-
A = 5.
fiveOrTen(A) :-
A = 10.

(Or just
fiveOrTen( 5).
fiveOrTen(10).
for this simple example.
)

But this might sometimes get a bit unwieldy, especially if the differing
parts that were abstracted out depended on many variables.
(Because you'd have to manually pass them other in arguments to the other
predicate. No local predicates (with non-local variable access) in Prolog
:-(
)

So, we can also solve this problem like :

myPrint(A) :-
( A = 5
; A = 10
),
print('Hello from the World to Benjamin').

Does that answer your question ?

> Thanks in advance
> Steve


--
Stefan Lj
md9slj

The infinity that can be finitely expressed is not the true infinity
Linux Man

2004-03-27, 12:11 am

Yes Stefan, Thanks a lot for your good answer.
seguso

2004-03-28, 10:29 pm

Linux Man wrote:

> if ( (A==5) || (A==10) )
> {
> print('Hello_from_the_World_to_Benjamin'
);
> }


__myPrint(A)_:-
__________(A==5 ; a==10),
__________print('Hello_from_the_World_to
_Benjamin').

--
Best Regards,
Maurizio Colucci
Please remove the uppercase letters "S,P,A,M":
seSgPuAsMo.forever@tin.it
Sponsored Links







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

Copyright 2008 codecomments.com