Re: RHS unbound variable
- From: LudovicoVan <julio@xxxxxxxxxxxxx>
- Date: Fri, 6 Feb 2009 10:23:43 -0800 (PST)
On 6 Feb, 18:20, LudovicoVan <ju...@xxxxxxxxxxxxx> wrote:
On 6 Feb, 14:50, LudovicoVan <ju...@xxxxxxxxxxxxx> wrote:
On 6 Feb, 14:08, Jose Luis <ingenieros.informat...@xxxxxxxxx> wrote:
Hi all,
Maybe, it is a so simple issue, but I am not sure if it is possible
for prolog resolve convert(C, 50) and convert(50, F) with a rule as
convert(Celsius, Fahrenheit) :-
Celsius is (Fahrenheit - 32) * 5 / 9.
I have read the solution in the Bratko book using { }, but it doesn't
work in SWI-Prolog
convert(Celsius, Fahrenheit) :-
{Celsius is (Fahrenheit - 32) * 5 / 9}.
Any suggestions?
Thanking you in advance
Hi Jose,
You might simply have two different clauses, checking with the var
predicate whether Celsius vs Farenheit is unbound, and performing the
calculation is the proper direction depending on the case:
conv(C, F) :- var(C), !, C is ...
conv(C, F) :- var(F), !, F is ...
FWIW, that's not really correct, in case both C and F are
instanciated. Here is the amended code:
conv(C, F) :- var(C), !, C is (F - 32) * 5/9.
conv(C, F) :- F is C * 9/5 - 32.
Sorry: F is C * 9/5 + 32.
-LV
Though the cut is no more green....
-LV
Otherwise, in SWI, you could use Clpqr as follows (although I wouldn't
know how portable it is):
:- use_module(library(clpr)).
conv(C, F) :- { C =:= (F - 32) * 5/9 }, !.
(I have added the cuts to avoid unneeded backtracking.)
HTH,
-LV
- References:
- RHS unbound variable
- From: Jose Luis
- Re: RHS unbound variable
- From: LudovicoVan
- Re: RHS unbound variable
- From: LudovicoVan
- RHS unbound variable
- Prev by Date: Re: RHS unbound variable
- Next by Date: Output of recursion
- Previous by thread: Re: RHS unbound variable
- Next by thread: Output of recursion
- Index(es):
Relevant Pages
|