For Programmers: Free Programming Magazines  


Home > Archive > Prolog > February 2005 > Help for PROLOG syntax









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 Help for PROLOG syntax
angtc11

2005-02-25, 9:00 pm

Hi

I am a beginner in PROLOG and have this question. How do I return a
'yes' or a 'no' togther with the answer to a variable?

For eg.

drink(coffee).
drink(tea).
drink(coke).

sweet(coke).

Lets say I wish to query drink(X) and it has to return whether it is
sweet, together with the name, like

X=coffee
no

X=tea
no

X=coke
yes

Is this possbile?
Thanks in advance!


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Björn Lindström

2005-02-25, 9:00 pm

angtc11@hotmail-dot-com.no-spam.invalid (angtc11) writes:

> Hi
>
> I am a beginner in PROLOG and have this question. How do I return a
> 'yes' or a 'no' togther with the answer to a variable?
>
> For eg.
>
> drink(coffee).
> drink(tea).
> drink(coke).
>
> sweet(coke).
>
> Lets say I wish to query drink(X) and it has to return whether it is
> sweet, together with the name, like
>
> X=coffee
> no
>
> X=tea
> no
>
> X=coke
> yes
>
> Is this possbile?
> Thanks in advance!


It'd be something like this:

drink(coffee, not_sweet).
drink(tea, not_sweet).
drink(coke, sweet).


You can then make a question such as:

?- drink(coffee, X).

X = not_sweet

Yes

--
Björn Lindström <bkhl@stp.ling.uu.se>
Student of computational linguistics, Uppsala University, Sweden
bow

2005-02-25, 9:00 pm

type in the following goal;

drink(D), sweet(D).


drink(D) will bind to 'coffee', but sweet(coffee) doesnt exist, so
backtracking will occur and bind drink(D) with drink(tea), but
sweet(tea) doesnt exist so backtracking will bind drink(D) with
drink(coke), and sweet(coke) exists so you will get the answer

brianh@metamilk.com

2005-02-26, 8:58 pm

test(X) :- drink(X), result(X).

result(X) :- sweet(X), !, write(X), write_ln(' is sweet').
result(X) :- write(X), write_ln(' is not sweet').

?- test(tea)

tea is not sweet

etc.
You can think of it like "test(X) succeeds if X is a drink and you can
print out some result about it, and result(X) succeeds either if X is
sweet or X is not sweet, printing out the result as a side effect" The
use of the cut (!) prevents the second clause for result being used if
X is sweet.

angtc11

2005-02-26, 8:58 pm

Thanks people!! Finally able to get the output needed :D


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Sponsored Links







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

Copyright 2008 codecomments.com