How to make this prolog program right

This is a discussion on How to make this prolog program right within the PROLOG forums in Programming Languages category; Hello, here is what I have done, basically, when I query valueEach([],_,[]). valueEach([H|T], Slot, [R|Rest]) :- value(H,Slot, V), !, % to skip execution of the next valueEach predicate, but the problem is it does not look for the alternative of value(indonesia, has_city, denpasar) in this query. R = [H,Slot,V], valueEach(T, Slot, Rest)...

Go Back   Application Development Forum > Programming Languages > PROLOG

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 06-28-2008, 04:26 AM
Keenlearner
Guest
 
Default How to make this prolog program right

Hello, here is what I have done, basically, when I query

valueEach([],_,[]).
valueEach([H|T], Slot, [R|Rest]) :-
value(H,Slot, V),
!, % to skip execution of the next valueEach predicate, but the
problem is it does not look for the alternative of value(indonesia,
has_city, denpasar) in this query.
R = [H,Slot,V],
valueEach(T, Slot, Rest)
Reply With Quote
  #2  
Old 06-29-2008, 10:31 AM
fodor.paul@gmail.com
Guest
 
Default Re: How to make this prolog program right

You have to collect all value/3 for same first argument.

valueEach([],_,[]).
valueEach([H|T], Slot, Result) :-
collectEach(H,Slot,[],L),
valueEach(T, Slot, Rest),
append(L,Rest,Result).

collectEach(H,Slot,PartialL,L):-
value(H,Slot,V),
\+(member([H,Slot,V],PartialL) ),
collectEach(H,Slot,[[H,Slot,V]|PartialL],L),
!.
collectEach(_H,_Slot,L,L).

value(indonesia,has_city,jakarta).
value(indonesia,has_city,denpasar).
value(usa,has_city,new_york).

?- valueEach([indonesia,usa,india],has_city,L).

Regards,
Paul.
Reply With Quote
  #3  
Old 06-29-2008, 02:09 PM
Keenlearner
Guest
 
Default Re: How to make this prolog program right

On Jun 29, 10:31*pm, "fodor.p...@gmail.com" <fodor.p...@gmail.com>
wrote:
> You have to collect all value/3 for same first argument.
>
> valueEach([],_,[]).
> valueEach([H|T], Slot, Result) :-
> * collectEach(H,Slot,[],L),
> * valueEach(T, Slot, Rest),
> * append(L,Rest,Result).
>
> collectEach(H,Slot,PartialL,L):-
> * value(H,Slot,V),
> * \+(member([H,Slot,V],PartialL) ),
> * collectEach(H,Slot,[[H,Slot,V]|PartialL],L),
> * !.
> collectEach(_H,_Slot,L,L).
>
> value(indonesia,has_city,jakarta).
> value(indonesia,has_city,denpasar).
> value(usa,has_city,new_york).
>
> ?- valueEach([indonesia,usa,india],has_city,L).
>
> Regards,
> Paul.


Thanks paul, I think that has solved my problem.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:00 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.